【发布时间】:2014-06-12 04:26:43
【问题描述】:
我是 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;
}
【问题讨论】:
-
如果你的“配方”是一个对象,也许你可以在它出现时将该对象传递给
NDetailViewController。然后,当您要关闭详细视图控制器时,将配方对象的名称属性(或您拥有的任何属性)设置为UITextField的文本。 -
请解释你的问题。您是否尝试将数据(在您的情况下为字符串)传递到详细信息视图?您还使用什么视图控制器?导航控制器 ?或 splitviewcontroller。
-
不,我想将数据从详细视图传递到主视图并更改表视图内容?
标签: ios objective-c uitableview