我是ios编程的新手;
我编写了一个应用程序以在UITableView
中显示食谱列表,在选择一行后,行标题将解析为详细视图中的UITextField
。
问题: 我可以做些什么来保存详细视图中的标题更改到表视图(我使用故事板)?
这是我的代码:
NViewController.m:
- (void)viewDidLoad
{
[super viewDidLoad];
recipes = [NSArray arrayWithObjects:@"Egg Benedict", @"Mushroom Risotto", @"Full Breakfast", nil];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"RecipeCell";
MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[MyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.lblName.text = [recipes objectAtIndex:indexPath.row];
return cell;
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"showRecipeDetail"]) {
NSIndexPath *indexPath = [self._tableView indexPathForSelectedRow];
NDetailViewController *destViewController = segue.destinationViewController;
destViewController.selectedRowTitle = [recipes objectAtIndex:indexPath.row];
}
}
NDetailViewController.m:
@interface NDetailViewController ()
@property (weak, nonatomic) IBOutlet UITextField *txtDetailText;
@end
@implementation NDetailViewController
@synthesize selectedRowTitle;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.txtDetailText.text = selectedRowTitle;
}
答案 0 :(得分:1)
在NViewController中,添加NSIndexPath以保存:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"showRecipeDetail"]) {
NSIndexPath *indexPath = [self._tableView indexPathForSelectedRow];
NDetailViewController *destViewController = segue.destinationViewController;
destViewController.selectedRowTitle = [recipes objectAtIndex:indexPath.row];
// need to save this indexPath to update when textfield is changed. Because indexPath maybe change if data changes, I think you should choose another field (like KEY, distingue objects)
destViewController.indexPath = indexPath;
destViewController.delegate = self;
}
}
-(void)changeTextFieldWithIndexPath : (NSIndexPath*)indexPath content:(NSString*)content;
{
// you update indexPath to row, and reloadTable
}
NDetailViewController.h:
@protocol NDetailViewControllerDelegate <NSObject>
-(void)changeTextFieldWithIndexPath : (NSIndexPath*)indexPath content:(NSString*)content;
@end
@interface NDetailViewController : ViewController
@property (nonatomic, weak) id<NDetailViewControllerDelegate> delegate;
@end
NDetailViewController.m:当textfiled更改值时,我的方法只是一个例子:
- (void) whenContentOfTextFieldChanged
{
[self.delegate changeTextFieldWithIndexPath:indexPath content:@"content which textfield is changed"];
}
答案 1 :(得分:0)
假设您正在使用UISplitview控制器,您可以通过[self.splitviewcontroller.viewcontrollers objectatindex:0]访问masterview并在其上调用已定义的方法。
您也可以使用通知传递数据 请查看此答案以获取通知示例:How to pass object with NSNotificationCenter