我有一个UIViewController
和一个UITableViewController
在ViewController中,我有UIButton
和UITextfield
,当我按下按钮时,它会导航到TableViewController
,其中包含桌面视图中的静态数据(iPhone 5S,iPhone 6,iPhone 6S,iPhone 7) ,当我选择任何一行时,它应该显示在ViewController的文本字段中。试过但不能得到这一个。帮我解决这个问题。下面是我的代码:
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 :(得分:1)
尝试使用NSNotification
。使用ViewController
将数据传回NSNotification
。首先在viewWillAppear
的{{1}}中注册通知。
请尝试以下代码:
在ViewController
:
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's
方法中,发布通知:
didSelectRowAtIndexPath
确定,ViewController和。中的通知名称必须相同 TableViewController
答案 1 :(得分: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
<{1>}文件中的
ViewControllerB.m
答案 2 :(得分:0)
按照:
在AppDelegate.h
@property (strong, nonatomic) NSString *selectedText;
在ViewController.m
中-(void)viewWillAppear:(BOOL)animated
{
AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
txtOne.text = appDelegate.selectedText;
}
导航到UITableViewController类时创建appDelegate.selectedText = @""
。
在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类中存储选定的值。(App delegate)。您可以在ViewController(viewWillAppear)中从中获取数据。