Reputation: 22236
Instances of UIButton, UITableViewCell, etc can be used as segue sources in Interface Builder (i.e. you can ctrl-click those objects and link them to controllers in a storyboard).
Can we create a direct UIView subclass that allows the same functionality? If so, how?
NOTE: Here's another way to ask my question: how can I add items in the "Triggered Segues" section of my own classes? Here's what one can see for a UITableViewCell:
Upvotes: 6
Views: 1149
Reputation: 21
You can subclass the control which fits better to your purpose.
Latter in Xcode you must place the name of the class in the right inspector and if it needs different options use key-propertyes.
The tab once you have selected the UIView is tooltiped as 'Show identity inspector'.
Then you can acces to the Custom Class area and you can select the class name.
A more sophisticated feature is using 'User Defined Runtime Attributes'
Upvotes: 0
Reputation: 96
As I liked to have the same functionality as Jean, I was puzzled. Disclaimer: I'm a total noob on iOS and Apple, so I assume it must be me and my lack of understanding why it's not working.
This 'answer' is more a theory, and maybe a very bad one. So please let me know if there' already a solution.
Theory
I do not think it is possible to extend/control the 'Triggered Segues' section via code alone. It seems like XCode knows based on the control chosen from the Object Library which Triggered Segues to display.
Thoughts
Having only a subclass of e.g. UIButton did not get me the Triggered Segues Section in the Connections Inspector. Setting the class in the Custom Class section does not help either.
Only when I use a Button from the Object Library, I get the Triggered Segues Section. E.g. if I subclass from UIButton, I should use a 'Button'.
I played around with the XML in my Main.storyboard - whenever I changed a <subviews><button ...
to a <subviews><view ...
the 'Triggered Segues' section is gone. Change it back and it appears. See below. I changed the element which contains the customClass="IconButton"
attribute from button to view.
Button: Having Triggered Segues
<viewController title="..." id="Zzf-WE-vxI" customClass="MyCustomClass" ... >
<view key="view" contentMode="scaleToFill" id="Mfn-O2-CMJ">
<!-- ... -->
<subviews>
<button customClass="IconButton" customModule="...">
<!-- ... -->
</button>
View: No Triggered Segues
<viewController title="..." id="Zzf-WE-vxI" customClass="MyCustomClass" ... >
<view key="view" contentMode="scaleToFill" id="Mfn-O2-CMJ">
<!-- ... -->
<subviews>
<view customClass="IconButton" customModule="...">
<!-- ... -->
</view>
Conclusion
My conclusion is that XCode shows Triggered Segues section based on the element/control in a storyboard's XML - it is not dependent on the class I subclass from. So I doubt it's possible to control the contents of the Triggered Segues section by code.
Please let me know if I'm wrong. As said, I've got no real experience with the internals of XCode right now and I'm curious if my theory is just bla bla.
Upvotes: 3
Reputation: 982
Yes from view controller to view controller create a segue in storyboard. YOU MUST give that segue an identifier. Then listen for touches began on that view and call [self performSegueWithIdentifier@"YOUR IDENTIFER"];
No you can not directly make a UIVIEW have an action. You can add a button HERE ADD A BUTTON TO VIEW
UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 100)];
[view setUserInteractionEnabled:NO];
[view setBackgroundColor:[UIColor redColor]];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn addSubview:view];
[btn setFrame:CGRectMake(0, 0, 200, 100)];
[btn addTarget:self action:@selector(click)forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
or add tap recognizer to view
//The setup code (in viewDidLoad in your view controller)
UITapGestureRecognizer *singleFingerTap =
[[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(handleSingleTap:)];
[self.view addGestureRecognizer:singleFingerTap];
//The event handling method
- (void)handleSingleTap:(UITapGestureRecognizer *)recognizer {
CGPoint location = [recognizer locationInView:[recognizer.view superview]];
//Do stuff here...
}
Upvotes: -1