2016-12-15 44 views
0

我有一个UIViewController和一个UITableViewController
在ViewController中,我有一个UIButtonUITextfield,当我按下按钮时,它将导航到TableViewController与表视图中的静态数据(iPhone 5S,iPhone 6,iPhone 6S,iPhone 7),当我选择任何行应该显示在ViewController的文本框中。试过但不能得到这一个。帮我解决这个问题。下面是我的代码:将数据从tableview传回给viewcontroller文本区目标 - C

ViewController.h 
———————————————— 
#import <UIKit/UIKit.h> 
#import "Utility.h" 

@interface ViewController : UIViewController 
- (IBAction)oneButtonClicked:(id)sender; 
@property (strong, nonatomic) IBOutlet UITextField *txtOne; 


ViewController.m 
———————————————— 

//Can't understand what to do in this viewcontroller. 

TableViewController.h 
—————————————————————- 
#import <UIKit/UIKit.h> 
#import "Utility.h" 
@interface FamilyDetailsViewController : UITableViewController 
@end 


TableViewController.m 
—————————————————————- 

#import "FamilyDetailsViewController.h" 
@interface FamilyDetailsViewController() 
{ 
    NSArray *arr; 
} 
@end 

@implementation FamilyDetailsViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    arr=[[NSArray alloc]initWithObjects:@"Parent",@"Grandparent",@"Sibling",@"Child",@"Other", nil]; 
} 

#pragma mark - Table view data source 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 

    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 

    return [arr count]; 
} 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 
    cell.textLabel.text=[arr objectAtIndex:indexPath.row]; 


    return cell; 
} 


#pragma mark - Table view delegate 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

    [self.navigationController popViewControllerAnimated:YES]; 
} 
+0

参考http://stackoverflow.com/questions/19343519/pass-数据返回先前视图控制器 – suhit

+1

足够的搜索后发布问题。你的问题有一千个解决方案。 [This](http://stackoverflow.com/questions/626898/how-do-i-create-delegates-in-objective-c)将帮助你。 – iPeter

+0

自定义协议是在视图控制器之间传递数据的最佳方式,因为peter提供链接和suhit的答案。 –

回答

0

关注这个:

在AppDelegate.h

@property (strong, nonatomic) NSString *selectedText; 

在ViewController.m

-(void)viewWillAppear:(BOOL)animated 
{ 
    AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate; 

    txtOne.text = appDelegate.selectedText; 

} 

appDelegate.selectedText = @""而导航到UITableViewController类。

在FamilyDetailsViewController.m

编译标记 - 表视图代表

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

     NSString *selected= [arr objectAtIndex:indexPath.row]; 

     AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate; 

     appDelegate.selectedText = selected; 

     [self.navigationController popViewControllerAnimated:YES]; 
} 

以这种方式要存储在Singleton类选定值(应用程序代表)。你可以从ViewController中取数据(viewWillAppear)。

+0

另外,还有一些方法可以在先前的ViewController中填充数据。 块,代表,NSNotificationCenter。 正如你所说,你是iOS开发新手。所以我告诉过你使用单例类的最简单的方法。 – SNarula

1

尝试使用NSNotification。将数据传回ViewControllerNSNotification。首先在ViewControllerviewWillAppear中注册通知。 试试下面的代码:

ViewController

-(void)viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear:YES]; 
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(getTextFromTable:) name:@"PassTextBackToViewController" object:nil]; 
} 

然后在ViewController定义方法是这样的:

-(void)getTextFromTable:(NSNotification *)notification 
{ 

    [self.textField setText:[notification.userInfo valueForKey:@"cellText"]]; 
} 

而且在tableView'sdidSelectRowAtIndexPath方法,邮政通知:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *cell=[tableView cellForRowAtIndexPath:indexPath]; 
    NSDictionary *dictionary=[NSDictionary dictionaryWithObjectsAndKeys:cell.textLabel.text,@"cellText", nil] 
    [[NSNotificationCenter defaultCenter]postNotificationName:@"PassTextBackToViewController" object:dictionary]; 
    [self.navigationController popViewControllerAnimated:YES]; 
} 

确认,通知的名字必须在两个视图控制器相同, TableViewController

1
in `ViewControllerB.h` which is a TableViewController declare a delegate to pass the data to ViewControllerA and create a delegate object 

#import <UIKit/UIKit.h> 

@protocol DataDelegate <NSObject> 

- (void)passData:(NSString *)data; 

@end 

@interface ViewControllerB : UIViewController 

@property (weak, nonatomic) id<DataDelegate> delegate; 

@end 

ViewControllerB.m文件

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

     NSString *selectedText = titleArray[indexPath.row]; 

     if [self.delegate respondsToSelector:@selector(passData:)]) { 
      [self.delegate passData:"hello"]; 
     } 

     [self.navigationController popViewControllerAnimated:YES]; 
} 



**in ViewControllerA** 

#import "ViewControllerA.h" 
#import "ViewControllerB.h" 

@interface ViewControllerA() <DataDelegate> 
@property (strong, nonatomic) ViewControllerB *viewControllerB; 
@end 

@implementation ViewControllerA 

- (void)viewDidLoad { 
    _viewControllerB.delegate = self; 
} 

- (void)passData:(NSString *)data { 
    self.textField.text = data 
} 
相关问题