【发布时间】:2013-07-24 22:28:44
【问题描述】:
所以目前我有一个 tableviewcontroller,如果发生某种情况,它会以模态方式显示在 loginviewcontroller 上方,然后一旦用户按下单元格,标题应该传递给父视图控制器。
所以图片 (A) 是父视图 - 主视图,(B) 是登录,(C) 是 tableviewcontroller...
C 位于 B 之上,B 位于 A 之上,不是通过导航堆栈而是通过模态堆栈。所以 C 除了这个之外没有任何对 A 的引用:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self Login];
[self.presentingViewController.presentingViewController dismissViewControllerAnimated:YES completion:nil];
}
当用户按下一个单元格时,视图会直接转到 (A),因此从 (C) 到 (A) 会跳过 (B)。这就是我想要的......但问题是我如何将数据传递给(A)而不必再次实例化(A)?我的意思是再次实例化,它已经在应用程序启动开始时实例化,如果用户尚未登录,则 (B) 显示在 (A) 上。所以 (A) 总是加载,但我如何传递数据给它?
如果有更多信息需要提供,请告诉我,我会尽快编辑。
编辑 我看了几个与这个相关的问题,我会使用 NSNotification 吗?还是委托?我试图同时使用这两种方法,但都没有真正起作用......我参考了我之前回答的关于委托的问题来实施委托解决方案,但我必须在 (A) 中实例化 (C)。但是我已经在 (B) 中实例化了 (C)。这是我要使用但没有使用的委托解决方案:Delegation。这是我尝试使用的 NSNotification 解决方案的链接:
把它放在你的父控制器中的 viewDidLoad
- (void) viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
[self makeCallbacks];
// get register to fetch notification
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(yourNotificationHandler:)
name:@"MODELVIEW DISMISS" object:nil];
}
//Now create yourNotificationHandler: like this in parent class
-(void)yourNotificationHandler:(NSNotification *)notice{
str = [notice object];
}
将以下内容放在您的子类中
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSLog(@"%@", cell.textLabel.text);
// cell.textLabel.text logs this -> EDF ENRS
[[NSNotificationCenter defaultCenter] postNotificationName:@"MODELVIEW DISMISS" object:cell.textLabel.text];
[self.presentingViewController.presentingViewController dismissViewControllerAnimated:YES completion:nil];
}
当我记录 'str' (null) 时会打印出来
现在的问题是,当我稍后在 viewWillAppear 中将 str 记录到通知之外时,它一直返回 null ......只有当我在通知方法中记录 str 时它才返回 null ......我会尝试解决这个问题稍后出,但我想主要问题是使用 NSNotificationCenter 解决了。
【问题讨论】:
-
为什么 NSNotificationCenter 不起作用?
-
您最好的选择是最有可能使用 Unwind Segues。这是一个非常深入的Q&A on StackOverflow regarding Unwind Segues
-
我没有使用故事板...我可以使用没有故事板的segues吗?抱歉,如果允许的话,那可能是一个愚蠢的 Q:/
标签: iphone ios objective-c uiviewcontroller delegates