开发者

iOS - adding a button to the view of a subview, but calling button's action on top view's controller

开发者 https://www.devze.com 2023-02-08 15:07 出处:网络
I\'ve added a view (button) onto a subview (map) of the top view on my app, but I need to call an action on the top view\'s controller when the button is tapped. Here\'s a breakdown of the layers:

I've added a view (button) onto a subview (map) of the top view on my app, but I need to call an action on the top view's controller when the button is tapped. Here's a breakdown of the layers:

-ViewController.view has subview of map.

-map has subview of button.

-button needs to have target/action with target being ViewController.

All of that is done in the ViewController programmatically, so map is a direct subview of ViewController.view (ie, it does not have it's own view controller).

When I make the button and set the target to self, it calls the selector action on the map. The button is a crosshair that should zoom into the user's location. I need it on the map for clipping and rotating purposes. If I add the view as a subview of ViewController with target self, the action fires and the map zooms in. If I add the view as subview of map, the action does not fire or throw any errors.

Is this possible?

Here is what I want to do in the ViewController:

[crosshair addTarget:self action:@selector(zoomToLocation) forControlEvents:UIControlEventTouchUpInside];

[[self map] addSubview:c开发者_运维百科rosshair];


You say: "When I make the button and set the target to self, it calls the selector action on the map." Where is this code, i.e., what is 'self' at that time?

What exactly is the problem with calling the selector in the map view? If it is that some work must be done by the view controller, you can set up a delegate (callback) function in the view controller that the selector calls.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号