【问题标题】:Variables in different navigation stack不同导航堆栈中的变量
【发布时间】:2011-08-25 22:45:04
【问题描述】:

对不起,如果我的问题与标题不匹配,但我认为它是: 我有一个带有导航控制器和 3 个视图的应用程序。这是一个应用程序,您可以在其中连接到服务器并与朋友交谈。 问题是,我的登录屏幕 rootViewController 调用了第二个视图,即好友列表。单击列表中的朋友会将您带到第三个视图,即聊天屏幕本身。 我想,当用户登录(rootview)时,将使用的用户名存储在一个变量中,这样我就可以在第三个视图,聊天屏幕上使用它,所以当他发送消息时,它可以有他的名字,并且在从服务器检索信息时,我也可以使用他的名字作为参数。 顺便问一下,SQLite 是保存消息和用户的最佳方式吗?我害怕核心数据 =/

【问题讨论】:

    标签: objective-c ios xcode nsdictionary


    【解决方案1】:

    为了保持松散耦合,我会考虑在实例化每个 UIViewController 子类时只传递 userName。 (也使单元测试更容易)

    例如

     // LoginViewController -> user logs in
     FriendsViewController *friendsViewController = [[FriendsViewController alloc] initWithUserName:userName];
     [self.navigationController pushViewController:friendsViewController];
     [friendsViewController release]; friendsViewController = nil;
    
     // FriendsViewController -> user selects a friend
     ChatViewController *chatViewController = [[ChatViewController alloc] initWithUserName:userName];
     [self.navigationController pushViewController:chatViewController];
     [chatViewController release]; chatViewController = nil;
    

    不要害怕核心数据,那里有很多优秀的书籍。

    【讨论】:

    • 谢谢,我刚刚发现如何使用应用程序委托来使用我在那里设置的变量。在你写的这个方法中,有没有例如“initWithPassword”?
    • 这通常被认为是不好的做法,因为您只是在创建一个全局变量。
    • initWithUserName 将是您必须编写的自定义方法,因此您也将使用 initWithPassword 方法。
    【解决方案2】:

    核心数据还不错。 This 是我开始使用 Core Data 的指南。一旦你了解了一切是如何运作的,它真的还不错。

    如果您愿意,可以使用 SQLite,但代码量很大。 Core Data 使用起来要简单得多。

    【讨论】:

      猜你喜欢
      • 2020-11-09
      • 1970-01-01
      • 2014-08-05
      • 2019-03-08
      • 1970-01-01
      • 1970-01-01
      • 2020-01-28
      • 2021-12-05
      • 1970-01-01
      相关资源
      最近更新 更多