当使用 [[UIApplication sharedApplication] delegate] 时
我不知道何时或为何应该使用 [[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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
[[UIApplication sharedApplication] delegate]
将为您提供指向您的应用程序委托的指针。当您创建项目时自动创建的一个。通常它被命名为YourAppNameAppDelegate
。您的代码有两件事有点问题。
当您声明
MyView *delegate
时,这应该是id delegate
或YourAppNameAppDelegate *delegate
。您不必通过应用程序委托访问您的 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 namedYourAppNameAppDelegate
.two things are a little wrong with your code.
when you declare
MyView *delegate
this should either beid delegate
orYourAppNameAppDelegate *delegate
.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.
当您在 IVar 中创建一个名为 appDelegate 的变量时,它不会自动设置为应用程序委托 - 您必须自己设置它。从应用程序中的任何地方,您都可以通过应用程序单例访问委托。
[UIApplication sharedApplication]
<--- 提供对单例委托方法的访问,返回指向应用程序委托的指针。
因此,由于您的变量需要初始化,因此有两个逻辑位置可以执行此操作 - 如果您创建一个变量,则在初始化程序中,或者在视图及其控制器的使用开始时调用的方法中,并且该方法地点是在viewDidLoad中。
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 singletondelegate 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.