【问题标题】:Passing value from Modal View to the Tab Bar Controller将值从模态视图传递到选项卡栏控制器
【发布时间】:2011-07-28 19:47:35
【问题描述】:

我正在实现一个 iPhone 应用程序,它是一个基于标签栏的应用程序。一开始,我抛出了一个模态视图,它显示了一个登录表单。如果登录正常,我将关闭此模式视图,并显示选项卡栏导航的第一个选项卡。问题是我需要将用户信息从模态视图传递到选项卡栏的控制器。 到目前为止,我得到的是: 在我的 AppDelegate 中:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

tabBarController.delegate = self;
// Add the tab bar controller's view to the window and display.
self.window.rootViewController = self.tabBarController;
[self addTabBarArrow];
[self.window makeKeyAndVisible];

//Throw up the Modal View

InicioAppModalViewController *tempView = [[InicioAppModalViewController alloc] initWithNibName:@"InicioAppModalView" bundle:nil];
[self.tabBarController presentModalViewController:tempView animated:true];
[tempView release];
return YES;}

在我的 InicioAppModalViewController 中,我有 2 个函数可以通过单击导航按钮项来显示带有登录表单的模式视图:

- (IBAction)showModalLoginForm:(id)sender {
LoginViewController * loginVC = [[LoginViewController alloc] initWithNibName:@"LoginView" bundle:nil];
[self presentModalViewController:loginVC animated:YES];
[loginVC release]; }

然后,在这个表单中,我刚刚获得了一些 TextField,我使用 NSURLConnection 方法验证登录是否正常。之后,在关闭此模式视图之前,我想将用户信息发送到导航栏控制器。

我该如何实现它?有什么建议吗?

提前致谢!!

【问题讨论】:

    标签: objective-c xcode cocoa-touch cocoa


    【解决方案1】:

    最简单的方法是使用Dependency injection,这意味着将此信息注入您的控制器,Apple 建议使用委托。

    所以你要做的就是在你的标签栏中定义一个属性来保存你想要传递的用户信息然后做这样的事情:

    [myTabBar new];
    [myTabBar setUserInfo:myUserInfo];
    

    【讨论】:

    • 我宁愿使用委托。我在网上看过一些教程,但任何人都有模态视图和标签栏视图分布。
    • @Ruben 现在更改了我回答的措辞 :)
    • :) 你是对的,“Apple 推荐”......你能检查一下我在我的问题中所做的编辑吗?也许你可以更好地帮助我
    【解决方案2】:

    我建议使用委托。在您的模态视图中,有一个属性:

    @property (nonatomic, assign) id delegate;
    

    然后将这一行放在 presentModalViewController 之前:

    loginVC.delegate = self;
    

    然后你可以实现一个方法,比如 collectUserData:(NSDictionary *)information 并在你释放它之前在 loginVC 中调用它:

    [delegate collectUserData:allMyInfoThatIwant];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-06-08
      • 2017-11-15
      • 2012-05-26
      • 2012-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多