简体   繁体   中英

UIButton addTarget in different ViewController

In my project, I have two ViewControllers - mapViewController and dataViewController.

In mapViewController, I have outlets for two buttons :

@property (weak, nonatomic) IBOutlet UIButton *previousButton;
@property (weak, nonatomic) IBOutlet UIButton *nextButton;

For fetching mapViewController in dataViewController,

 self.MapViewController = ((OTPAppDelegate *)[[UIApplication sharedApplication] delegate]).mapViewController;

Using the above technique, I can manipulate the properties of mapViewController inside dataViewController by accessing self.MapViewController.property

However, if I wish to add a target for the two buttons inside dataViewController using the following code:

 [self.MapViewController.previousButton addTarget:self action:@selector(doNothing:) forControlEvents:UIControlEventTouchDown];

It throws a BAD access error. I was wondering what needs to be fixed, in order to achieve the desired button click behavior.

Create a protocol in MapViewController

@Protocol prtocol_name <NSObject>{
 -(void)method_name;
@end

create an object for protocol in MapViewController.

@property(nonatomic) id< prtocol_name> delegate;

in button methods implementation call protocol method like following

[self.delegate method_name];

And finally implement protocol method in DataViewController.

Thanks

If you want the target/selector in different view controller, then pass the delegate parameter as other view controller's instance. For eg:

     [self.MapViewController.previousButton addTarget:otherControllerInstance     action:@selector(doNothing:)

forControlEvents:UIControlEventTouchDown];

Detailed Explanation:- You have two classes named FirstVC and SecondVC. A button is present in FirstVC, on which you want to add target in SecondVC.

[button addTarget:objSecondVC action:@selector(doSomething:)
   forControlEvents:UIControlEventTouchDown];

I hope you have create property of mapViewController into dataViewController.

If you choose wrong property attributes then it can raise error you got.

Another possibility is, MapViewController property not assigned/initialized properly and it is nil while you are trying to add target of its subview.

The best way to get callback event is to use delegate.

Below are some information on how delegate works:

Delegate are function pointers. Using it, one can call another class' function easily.

To create delegate, common procedure is to, first create protocol and add relevant methods in it (in the class you want to initiate delegate method). This methods can be implemented by class that adopts protocol.

You also need to create generic property of protocol type called delegate property. This will be assigned to instance of class that conforms to protocol.

In your case, class mapViewController define some protocol in it. Here, dataViewController conforms class mapViewController 's protocol.

Now, class dataViewController has object defined of class mapViewController in it. In class dataViewController , here we need to assign class mapViewController 's delegate to instance of dataViewController (self). (now in class mapViewController , delegate property contains instance of dataViewController and one can easily call protocol method implemented in class dataViewController from class mapViewController ).

I hope this will help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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