简体   繁体   English

Objective-C:如何调用实例方法

[英]Objective-C: How to Call a Instance Method

I need to call a method I have defined as: 我需要调用定义为的方法:

-(IBAction)next:(id)sender{ 
...

}

I want to call it in -[UIViewController viewDidload] 我想在-[UIViewController viewDidload]调用它

How can I call this method programmatically? 如何以编程方式调用此方法?

[self next:nil];
  • self is the object receiving the message, assuming -next: is defined in the same class as -viewDidLoad . self是接收消息的对象,假定-next:-viewDidLoad在同一类中-viewDidLoad
  • next: is the name of the message (method). next:是消息的名称(方法)。
  • Since you don't use sender , pass the argument nil , meaning "nothing". 由于您不使用sender ,因此请传递参数nil ,表示“无”。

If -next: is defined in the App delegate but -viewDidLoad in some view controller, use 如果-next:是在应用程序委托中定义的,但在某些视图控制器中是-viewDidLoad ,请使用

[UIApplication sharedApplication].delegate

to refer to the app delegate. 引用应用程序委托。 So the statement becomes 因此,该语句变为

[[UIApplication sharedApplication].delegate next:nil];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM