Cannot Send Message to Other ViewController

北城余情 提交于 2019-12-12 01:31:19

问题


I was messing around in Xcode creating... something... when I needed to send a message to the MainViewController.m from the FlipsideViewController.m. Except, when I do, the ViewController I am sending the message from does not detect the method as existing, despite the fact that I put the method in MainViewController.h. This is the method:

- (void)setAutosave:(BOOL)boolee {
    _autosave = boolee;
}

I have imported the MainViewController into the FlipsideViewController, but as I call it ([NSMainViewController setAutosave:_autosave];), it simply throws an error:

No known class method for selector 'setAutosave:'

I am doing this because FlipsideViewController has a segmented control in it, which tells autosave to be on or off, but it needs to send it's value to the other ViewController.

I really am stumped, help is appreciated.


回答1:


MainViewController is a class, you can't access instance method from a class.

You can add a property to FlipsideViewController, for example:

@property (weak, nonatomic) MainViewController *mainViewController;

Assign your MainViewController instance to mainViewController (how to assign depends on the relationship between your two view controllers), then you can invoke that method by [self.mainViewController setAutosave:_autosave];.



来源:https://stackoverflow.com/questions/21058039/cannot-send-message-to-other-viewcontroller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!