我在AppDelegate.h文件中定义了一个NSMutableString。
//AppDelegate.h
@interface AppDelegate : UIResponder <UIApplicationDelegate>{
@public
NSMutableString *nidForDetailDisplay;
}
然后我在ViewController.m文件中设置了这个字符串。我存储一个字符串(nidForDetailDisplay)从tableview传递到detailview。
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
rowNumber = indexPath.row;
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
// this is where I set the string to a string of text
appDelegate->theNidForDetailDisplay = [arrayOfNids objectAtIndex:rowNumber];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
UIViewController *detailViewController;
DetailViewController *detail = [self.storyboard instantiateViewControllerWithIdentifier:@"detail"];
[self.navigationController pushViewController:detail animated:YES];
}
在ViewController中,当我记录NidForDetailDisplay时,我得到了正确的字符串。
接下来,我尝试在随后的DetailViewController中访问此更新的NSMutableString(nidForDetailDisplay)
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
NSMutableString *storyid;
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
appDelegate->theNidOnDetailDisplay = storyid;
}
但是,当我打印字符串(storyid)或(theNidOnDetailDisplay)时,它们都返回null。
我在这里遗漏了什么吗?我应该使用实例变量吗? 提前致谢。
答案 0 :(得分:0)
不要使用AppDelegate传递数据。将属性添加到详细视图控制器并将值分配给属性。
on DetailViewController
:
@property (nonatomic) NSMutableString * nid;
在您出席之前分配:
DetailViewController *detail = [self.storyboard instantiateViewControllerWithIdentifier:@"detail"];
detail.nid = [arrayOfNids objectAtIndex:rowNumber];
[self.navigationController pushViewController:detail animated:YES];