不同导航堆栈中的变量

发布于 2024-12-01 06:08:49 字数 294 浏览 1 评论 0原文

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

Sorry if my question does not match the title, but i think it does:
I have an app with a navigation controller and 3 views. It's an app where you connect to a server and talk with friends.
The thing is, my rootViewController, the login screen, calls the second view witch is the friends list. clicking in a friend on the list takes you to the third view, the chat screen itself.
I wanted to, when the user logs in (rootview), to store the user name used in a variable, so i could use it on the third view, the chat screen, so when he sends a message it can have his name on it, and while retrieving info from server, i can use his name as a parameter too.
By the way, is SQLite the best way to save messages and users? I am afraid of Core Data =/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

停滞 2024-12-08 06:08:49

为了保持松散耦合,我会考虑在实例化每个 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;

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

To keep things loosely coupled I would consider just passing along the userName when instantiating each UIViewController sub class. (Makes unit testing easier as well)

e.g.

 // 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;

Don't be afraid of core data there are plenty of excellent books out there.

痞味浪人 2024-12-08 06:08:49

核心数据并没有那么糟糕。 这是我用来开始使用 Core Data 的指南。一旦你了解了一切是如何运作的,事情其实并没有那么糟糕。

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

Core data is not that bad. This is the guide I used to get started with Core Data. Once you wrap your head around how everything works it really isn't that bad.

If you wanted to you could use SQLite, but it's a lot of code. Core Data is much simpler to use.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文