当使用 [[UIApplication sharedApplication] delegate] 时

发布于 2024-10-04 08:46:34 字数 484 浏览 1 评论 0原文

我不知道何时或为何应该使用 [[UIApplication sharedApplication] delegate]

[self.navigationController PushViewController:myView Animation:YES] 不使用时,我使用 delegate导航。我制作 MyView *delegate = [[UIApplication shareApplication] delegate][delegate PushViewController:myView Animation:YES] 以使导航正常工作。

我不知道该解决方案是否正确,此外我不知道 [[UIApplication sharedApplication] delegate] 有什么用处,或者它对应用程序有什么影响。

有人可以帮助我吗?

谢谢!!

I don't know when or why I should use [[UIApplication sharedApplication] delegate]

I use delegate when [self.navigationController pushViewController:myView Animation:YES] does't navigate. I make MyView *delegate = [[UIApplication sharedApplication] delegate] and [delegate pushViewController:myView Animation:YES] to make the navigation work correctly.

I don't know if the solution is correct and in addition I don't know what is the use for [[UIApplication sharedApplication] delegate] or what are its effects on the application.

Can someone help me?

Thanks!!

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

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

发布评论

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

评论(2

壹場煙雨 2024-10-11 08:46:34

[[UIApplication sharedApplication] delegate] 将为您提供指向您的应用程序委托的指针。当您创建项目时自动创建的一个。通常它被命名为YourAppNameAppDelegate

您的代码有两件事有点问题。

  1. 当您声明 MyView *delegate 时,这应该是 id delegateYourAppNameAppDelegate *delegate

  2. 您不必通过应用程序委托访问您的 navigationController。我会研究 self.navigationController 失败的原因,并解决这个问题。当您依赖应用程序委托时,您基本上是在实现单例模式。因为单例模式是最不酷的编程设计模式,所以在与其他程序员讨论时它不会为您赢得任何分数。

[[UIApplication sharedApplication] delegate] will give you a pointer to your app delegate. the one that was automatically created when you made the project. Usually it's named YourAppNameAppDelegate.

two things are a little wrong with your code.

  1. when you declare MyView *delegate this should either be id delegate or YourAppNameAppDelegate *delegate.

  2. You shouldn't have to access your navigationController via the app delegate. I would look into why self.navigationController is failing, and address that. You're basically implementing a singleton pattern when you rely on the app delegate. Because the singleton pattern is the least cool programming design pattern, it won't win you any points when talking shop with other programmers.

燃情 2024-10-11 08:46:34
self.appDelegate = [[UIApplication sharedApplication] delegate];

当您在 IVar 中创建一个名为 appDelegate 的变量时,它不会自动设置为应用程序委托 - 您必须自己设置它。从应用程序中的任何地方,您都可以通过应用程序单例访问委托。

[UIApplication sharedApplication] <--- 提供对单例

委托方法的访问,返回指向应用程序委托的指针。

因此,由于您的变量需要初始化,因此有两个逻辑位置可以执行此操作 - 如果您创建一个变量,则在初始化程序中,或者在视图及其控制器的使用开始时调用的方法中,并且该方法地点是在viewDidLoad中。

self.appDelegate = [[UIApplication sharedApplication] delegate];

When you create a variable called appDelegate in your IVars, it won't just get set automagically to the app delegate - you have to set it yourself. From ANYWHERE in your application, you can access the delegate by the application singleton.

[UIApplication sharedApplication] <--- gives access to the singleton

delegate method returns a pointer to the app delegate.

So, since your variables needed to be initialized, there are two logical places to do it - one is in the initializer if you create one or in a method that is called at near the beginning of the use of a view and its controller and that place is in viewDidLoad.

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