Creating buttons in Xcode - ios

Is there a better way to do the following in Xcode for iPhone app?
I have a main navigation UIViewController screen with 4 buttons on it.
Clicking button 1 shows a sub navigation UIViewController with 4 buttons on it.
Clicking button 2 shows the same sub navigation UIViewController but with 5 buttons on it.
Clicking button 3 shows the same sub navigation UIViewController but with 4 buttons on it.
Clicking button 4 shows the same sub navigation UIViewController but with 6 buttons on it.
To handle this I have assigned tags to each button in the main navigation.
When a button is clicked, I take this tag number and pass it to sub navigation UIViewController.
Then in the sub navigation UIViewController, based on this value, I manually draw/create the sub navigation buttons as needed.
Below is how I handle this in the sub navigation UIViewController.
I check to see what value was passed from main navigation UIViewController, and draw the number of buttons accordingly. I also set custom background images for each button. And for each button click I have its own selector method. Note that some buttons in the sub navigation will go to a UIViewController while some buttons will go to a TableViewController. Also each sub navigation button will have to display its own "content" in its destination view.
Is there a better, more elegant way of handling this? It just seems like a lot of code duplication to me.The example below shortened for brevity.
// SubNavViewController.m
// SegueTest
#import "SubNavViewController.h"
#import "GettingHereViewController.h"
#import "TableViewController.h"
#import "GettingHereContent.h"
#interface SubNavViewController ()
#end
#implementation SubNavViewController
#synthesize tagNumber;
#synthesize buttonNumber;
#synthesize buttonPushedTrackNumber;
#synthesize hotelButton;
#synthesize bAndBButton;
#synthesize caravanButton;
#synthesize selfCateringButton;
#synthesize apartmentsButton;
.
.
.
etc (19 in total)
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
if (self.tagNumber == 1)
{
self.navigationItem.title = #"Getting Here";
// By Air Button
byAirButton = [UIButton buttonWithType:UIButtonTypeCustom];
byAirButton.tag = 1;
byAirButton.frame = CGRectMake(25, 140, 280.f, 40.f);
UIImage *airButton = [UIImage imageNamed:#"gettingHereByAirButton.png"];
[byAirButton setBackgroundImage:airButton forState:UIControlStateNormal];
[self.view addSubview:byAirButton];
[byAirButton addTarget:self action:#selector(byAirButtonClicked) forControlEvents:UIControlEventTouchUpInside];
// By Rail Button
byRailButton = [UIButton buttonWithType:UIButtonTypeCustom];
byRailButton.tag = 2;
byRailButton.frame = CGRectMake(25, 190, 280.f, 40.f);
UIImage *railButton = [UIImage imageNamed:#"gettingHereByRailButton.png"];
[byRailButton setBackgroundImage:railButton forState:UIControlStateNormal];
[self.view addSubview:byRailButton];
[byRailButton addTarget:self action:#selector(byRailButtonClicked) forControlEvents:UIControlEventTouchUpInside];
.
.
. etc (2 more button)
}
else if (self.tagNumber == 2)
{
self.navigationItem.title = #"Where to Stay";
// B&B Button
bAndBButton = [UIButton buttonWithType:UIButtonTypeCustom];
bAndBButton.tag = 1;
bAndBButton.frame = CGRectMake(25, 140, 280.f, 40.f);
UIImage *bedAndBreakfast = [UIImage imageNamed:#"whereToStayBedAndBreakfastButton.png"];
[bAndBButton setBackgroundImage:bedAndBreakfast forState:UIControlStateNormal];
[self.view addSubview:bAndBButton];
[bAndBButton addTarget:self action:#selector(bAndBButtonClicked) forControlEvents:UIControlEventTouchUpInside];
.
.
. etc (do this for the rest of the buttons)
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (void) byAirButtonClicked
{
GettingHereContent *gettingHere = [GettingHereContent new];
gettingHere = #"By Air";
NSLog(#"Content: %#", gettingHere);
[self performSegueWithIdentifier:#"gettingHereSegue" sender:self];
}
- (void) bAndBButtonClicked
{
GettingHereContent *gettingHere = [GettingHereContent new];
gettingHere = #"Bed and Breakfast";
NSLog(#"Content: %#", gettingHere);
[self performSegueWithIdentifier:#"tableViewSegue" sender:self];
}
.
.
. (do this for all buttons - 19 in total)
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
}
#end

First I would recommend to create a NS_ENUM defining all your buttons in the following way:
typedef NS_ENUM(NSUInteger, ButtonTag)
{
ButtonTagHotel,
ButtonTagOther,
...
}
Then in viewDidLoad: you could create an Array with all buttons, that are needed for your current state like the following way:
NSMutableArray *buttonTags = [[NSMutableArray alloc] init];
if (self.tagNumber == 1 || self.tagNumber == 3)
[buttonTags addObject: #(ButtonTagHotel)];
if (self.tagNumber == 5 || self.tagNumber == 8)
[buttonTags addObject: #(ButtonTagOther)];
// etc...
When you have build up your needed button tags, you can create and add them all in one loop:
uint cnt = 0;
for (NSNumber *tag in buttonTags)
{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.tag = [tag unsignedIntValue];
btn.frame = CGRectMake(25, 20 + (cnt * 50.f), 280.f, 40.f);
UIImage *image = [UIImage imageNamed:#"genericButtonImage.png"];
[btn setBackgroundImage:image forState:UIControlStateNormal];
[self.view addSubview:btn];
[btn addTarget:self action:#selector(buttonTouched:) forControlEvents:UIControlEventTouchUpInside];
cnt++;
}
If you have to set a different image for every button, then you should create another array that is holding all image names indexed by the ButtonTag enum...
Now you just have to implement -(void)buttonTouched:(UIButton*)sender:
-(void)buttonTouched:(UIButton*)sender
{
switch (sender.tag) {
case ButtonTagHotel:
{
// do your stuff
}
break;
...
default:
break;
}
}

Related

UIButton not working inside UITableView(crashes on tap)? Can I get some help for obj-c

SOLUTION: it was stupidly simple, I overlooked the need to declare the method for my button function in this implementation, the fix was simply adding this in the right spot (inside the implementation... duh!)
-(void)bugButton { //do stuff }
Found some code for a button that looked like what I wanted. After adding & compiling the button crashes my app when tapped, any idea why? Heres the entire cell, pretty new to making these so its hacked together from other stuff, the button stuff is near the top. I do have the method in a different place (and have used boring PSButtonCell's successfully so I know that the method works)
#interface harpButtonCell : PSTableCell <PreferencesTableCustomView> {
}
#end
#implementation harpButtonCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(id)reuseIdentifier specifier:(id)specifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier specifier:specifier];
if (self) {
// icon
UIImage *bugImage = [[UIImage alloc] initWithContentsOfFile:[NSString stringWithFormat:#"%#/Bug.png", kSelfBundlePath]];
UIButton *bugbutton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 75, 75)];
[bugbutton setImage:bugImage forState:UIControlStateNormal];
[bugbutton addTarget:self action:#selector(bugButton) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:bugbutton];
/*
UIImage *paypalImage = [[UIImage alloc] initWithContentsOfFile:[NSString stringWithFormat:#"%#/Paypal.png", kSelfBundlePath]];
UIButton *paypalbutton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 75, 75)];
[paypalbutton setImage:paypalImage forState:UIControlStateNormal];
[paypalbutton addTarget:self action:#selector(paypalButton) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:paypalbutton];*/
//int width = self.contentView.bounds.size.width;
}
return self;
}
- (instancetype)initWithSpecifier:(PSSpecifier *)specifier {
return [self initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"harpButtonCell" specifier:specifier];
}
- (void)setFrame:(CGRect)frame {
frame.origin.x = 0;
[super setFrame:frame];
}
- (CGFloat)preferredHeightForWidth:(CGFloat)arg1{
return 100.0f;
}
- (CGFloat)preferredHeightForWidth:(CGFloat)width inTableView:(id)tableView {
return [self preferredHeightForWidth:width];
}
#end
It seems that you don't implement the action of button, try to add the action in the cell:
-(void)bugButton{
NSLog(#"you clicked on button");
}

Correct use of Enum, Button always the same text

I didn't know where to search for my question, but I am very confused about this.
The premise is: I have one ViewController X that calls ViewController Y and depending on which button is selected in X, I want a different button title for ViewController Y.
In ViewController X, (SSPhotosSelectionView is ViewController Y in this case):
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
SSPhotoSelectionViewController *controller = [[SSPhotoSelectionViewController alloc] init];
if ([segue.identifier isEqualToString:#"SelectPhotosSegue"]) {
if (self.albumsButton.selected) {
[controller.downloadButton setText:SSPhotosButtonTextAdd];
}
else if (self.galleryButton.selected) {
[controller.downloadButton setText:SSPhotosButtonTextDownload];
}
}
}
As you can see from the code, if albumsButton is selected, I want the downloadButton in SSPhotoSelectionViewController to say "Add", otherwise, say "Download".
downloadButton in this case, is a SSPhotosButton object, which is a subclass of UIButton:
SSPhotosButton.h:
#import <UIKit/UIKit.h>
typedef NS_ENUM(NSInteger, SSPhotosButtonText) {
SSPhotosButtonTextDownload = 0,
SSPhotosButtonTextAdd = 1,
};
#interface SSPhotosButton : UIButton
#property (nonatomic, assign) SSPhotosButtonText text;
- (void)setText:(SSPhotosButtonText)text;
#end
SSPhotosButton.m:
#import "SSPhotosButton.h"
#implementation SSPhotosButton
- (id)initWithCoder:(NSCoder *)aDecoder {
if (self = [super initWithCoder:aDecoder]) {
self.layer.masksToBounds = YES;
if (self.text == SSPhotosButtonTextDownload) {
[self updateButtonWithText:#"Download"];
}
else if (self.text == SSPhotosButtonTextAdd) {
[self updateButtonWithText:#"Add"];
}
}
return self;
}
- (void)setText:(SSPhotosButtonText)text {
_text = text;
switch (text) {
case SSPhotosButtonTextDownload:
[self updateButtonWithText:#"Download"];
break;
case SSPhotosButtonTextAdd:
[self updateButtonWithText:#"Add"];
break;
default:
break;
}
}
- (void)updateButtonWithText:(NSString *)string {
self.titleLabel.text = string;
[self setTitle:string forState:UIControlStateNormal];
[self setBackgroundColor:[UIColor clearColor]];
[self setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[self setTitleColor:[UIColor colorWithWhite:0.75 alpha:1.0] forState:UIControlStateHighlighted];
[self setTitleColor:[UIColor colorWithWhite:0.75 alpha:1.0] forState:UIControlStateSelected];
[self setTitleColor:[UIColor colorWithWhite:0.25 alpha:0.5] forState:UIControlStateDisabled];
}
#end
My problem is: No matter which button I select, (albumButton or galleryButton), the text is always "Download", never "Add". I suspect I'm doing something wrong in my SSPhotosButton classes, which is SSPhotosButtonText is always 0, which is why it's always SSPhotosButtonTextDownload, but how can I fix this?
Thank you.
In prepareForSeque you never instantiate the destination view controller directly. Instead, you access it via the segue parameter:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
SSPhotoSelectionViewController *controller = (SSPhotoSelectionViewController *) segue.destinationViewController;
if ([segue.identifier isEqualToString:#"SelectPhotosSegue"]) {
if (self.albumsButton.selected) {
[controller.downloadButton setText:SSPhotosButtonTextAdd];
}
else if (self.galleryButton.selected) {
[controller.downloadButton setText:SSPhotosButtonTextDownload];
}
}
}
Doing it so should set your text correctly.

How to send a value to a ViewController when a button is pressed

I currently have two buttons in my SearchCategoryChooserViewController. What I want to do is to set the chosenCategory property to a certain value depending on which button is pressed, and then send that value over to CriteriaViewController.
I have some psuedo code commented out in my categoryButtonClick function, but I'm not sure how to format the syntax, and where to take it from there. The topCategoryId1 and topCategoryId2 values are coming from SearchViewController. Let me know if you want me to include code from that or any other classes.
SearchCategoryChooserViewController.m:
#import "SearchCategoryChooserViewController.h"
#import "SearchViewController.h"
#import "CriteriaViewController.h"
#interface SearchCategoryChooserViewController ()
#end
#implementation SearchCategoryChooserViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton *category1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
category1.frame = CGRectMake(10, 120, 300, 35);
[category1 setTitle: [NSString stringWithFormat:#"%#", self.topCategory1] forState:UIControlStateNormal];
[category1 addTarget:self action:#selector(categoryButtonClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview: category1];
UIButton *category2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
category2.frame = CGRectMake(10, 180, 300, 35);
[category2 setTitle: [NSString stringWithFormat:#"%#", self.topCategory2] forState:UIControlStateNormal];
[category2 addTarget:self action:#selector(categoryButtonClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview: category2];
}
- (IBAction)categoryButtonClick:(id)sender
{
// if (topCategory1 button is pressed) {
// set chosenCategory = self.topCategoryId1
// }
//
// else if (topCategory2 button is pressed) {
// set chosenCategory = self.topCategoryId2
// }
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Navigation
// Send the Category Id over to CriteriaViewController
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
CriteriaViewController *controller = (CriteriaViewController *) segue.destinationViewController;
// Send over the search query as well as the specific category to CriteriaVC to use
controller.chosenCategory = self.topCategoryId1;
}
#end
One possible solution would be to use tags. Set each button to have its own tag after creating it, for example:
UIButton *category1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
category1.frame = CGRectMake(10, 120, 300, 35);
[category1 setTitle: [NSString stringWithFormat:#"%#", self.topCategory1] forState:UIControlStateNormal];
[category1 addTarget:self action:#selector(categoryButtonClick:) forControlEvents:UIControlEventTouchUpInside];
category1.tag = 1; //added tag to button
[self.view addSubview: category1];
UIButton *category2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
category2.frame = CGRectMake(10, 180, 300, 35);
[category2 setTitle: [NSString stringWithFormat:#"%#", self.topCategory2] forState:UIControlStateNormal];
[category2 addTarget:self action:#selector(categoryButtonClick:) forControlEvents:UIControlEventTouchUpInside];
category2.tag = 2; //added tag to button
[self.view addSubview: category2];
Then in your button action method (categoryButtonClick:), check the button's tag like so:
- (IBAction)categoryButtonClick:(id)sender
{ UIButton *button = (UIButton *)sender;
if(button.tag == 1){
chosenCategory = self.topCategoryId1;
}else{
chosenCategory = self.topCategoryId2;
}
}
You could do:
- (IBAction)categoryButtonClick:(id)sender
{
if ([topCategory1 isSelected]) {
set chosenCategory = self.topCategoryId1
}
else if ([topCategory2 isSelected]) {
set chosenCategory = self.topCategoryId2;
}
}
In adition to the #croberth's answer, after conditionally setting chosenCategory, send a performSegueWithIdentifier:sender: message:
- (IBAction)categoryButtonClick:(id)sender
{
UIButton *button = (UIButton *)sender;
if(button.tag == 1) {
chosenCategory = self.topCategoryId1;
}
else {
chosenCategory = self.topCategoryId2;
}
[self performSegueWithIdentifier:#"YOUR_SEGUE_IDENTIFIER" sender:nil];
}
YOUR_SEGUE_IDENTIFIER is the identifier for your segue between the button and the CriteriaViewController. Since your buttons aren't in your storyboard, it should be a segue between the 2 controllers, not from any button.
What you're asking has been answered several times already on SO. You set up a variable in the sending VC in the prepareForSegue method and prepare a variable to receive that value in the receiving VC. Here's an SO link with a lot more detail: ios pass values during a segue to another view

How do I reuse the same code between files with the option to pass in arguments in objective-c?

I've noticed I have a set of UIViewController's for a product filtering system that use the exact same code. This code basically creates buttons for a UIToolBar and hides the UIToolBar of the controller that presented it. The only difference will be the title that says "refine by:.
On each page I'll have the type of page it is after "refine by" so for example: On the gender filtering page it will say "Refine by: then I'll append another string in bold that says "Gender" for the products type page I'll append a bold string that says "Product type" and so on.
Here is the code that is repeated in each of the custom classes connected to these controllers:
#property (weak, nonatomic) IBOutlet UIToolbar *toolBar;
#end
#implementation VAGRefineProductTypeViewController
{
VAGRefineProductTypeViewController *_thisController;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
// Custom initialization
_thisController = self;
}
return self;
}
- (void)viewDidLoad
{
UIToolbar *toolBar = [_thisController toolBar];
[[_thisController navigationItem] setLeftBarButtonItem:nil];
[_thisController setTitle:#"R E F I N E B Y:"];
// Setup buttons for tool bar
UIButton *clearButton = [[UIButton alloc] initWithFrame:CGRectMake(40, 3, 110, 38)];
UIButton *doneButton = [[UIButton alloc] initWithFrame:CGRectMake(170, 3, 110, 38)];
[clearButton setBackgroundColor:[UIColor darkGrayColor]];
[doneButton setBackgroundColor:[UIColor blackColor]];
[clearButton setTitle:#"Clear" forState:UIControlStateNormal];
[doneButton setTitle:#"Done" forState:UIControlStateNormal];
[clearButton addTarget:_thisController action:#selector(clearButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
[doneButton addTarget:_thisController action:#selector(doneButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
// Add buttons to toolbar
[toolBar addSubview:clearButton];
[toolBar addSubview:doneButton];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
// Hide previous pages tool bar
[[_thisController navigationController] setToolbarHidden:YES animated:NO];
}
- (IBAction)clearButtonTapped:(id)sender {
NSLog(#"CLEAR BUTTON TAPPED");
}
- (IBAction)doneButtonTapped:(id)sender {
NSLog(#"DONE BUTTON TAPPED");
}
This UIToolBar is added to each UIViewController in interface builder. The one that is hidden in viewWillAppear is the one added programmatically from UIViewController that presented the current UIViewController.
Question:
Rather than copying and pasting this code into each UIViewController's class file how can I put it in a file and use that file within each UIViewController?
You have various options. Probably the simplest is to define a base UIViewController class that defines the shared properties and methods, and then make all the view controllers that use these properties subclasses of that class.
Another option would be to define a category of UIViewController that defines new methods. That doesn't let you add new properties or instance variables to your view controllers however (at least not without getting tricky.)

RemoveFromSuperView not working on UIButton

It seems like such a simple thing to remove a button from a view, but it is not working.
MyViewController.h
#import <UIKit/UIKit.h>
#interface MyViewController : UIViewController
#property (strong, nonatomic, readonly, getter = getMyButton) UIButton* myButton;
- (id) init;
- (id) getMyButton;
#end
MyViewController.m
#import "MyViewController.h"
#interface MyViewController ()
#end
#implementation MyViewController
#synthesize myButton = _myButton;
- (id) init
{
if([super initWithNibName: nil bundle: nil])
{
_myButton = nil;
}
return self;
}
- (id) getMyButton
{
if(!_myButton) _myButton = [self createMyButton];
return _myButton;
}
- (void) viewDidLoad
{
[super viewDidLoad];
UIButton* myButton = self.myButton;
[self.view addSubview: myButton];
}
- (void) didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (UIButton*) createMyButton
{
UIButton *button = [[UIButton alloc] init];
[button setTitle: #"My Button"
forState: UIControlStateNormal];
[button addTarget: self
action: #selector(myAction:)
forControlEvents: UIControlEventTouchUpInside];
button.frame = CGRectMake(10, 10, 100, 40);
return button;
}
- (void) myAction: (id) sender
{
[self.myButton performSelectorOnMainThread: #selector(removeFromSuperview) withObject: nil waitUntilDone: NO];
}
#end
But no luck. Clicking the button simply does nothing.
If it is not a problem of concurrency then maybe it is a memory management problem? Maybe it is just something daft, I don't know.
I tried putting the following line into the myAction method
NSLog(#"Test 1");
if(_startButton.superview) NSLog(#"Test 2");
Only 'Test 1' is logged. Perhaps that is a clue but what I don't know is why the button has no superview when it is added to view and is visible on the screen
Additional information
I don't know, if any of this is relevant, but maybe
I just updated Xcode to the latest version from the developer program (it supported up to iOS version 7.0 before, now 7.1)
I just started testing the app on an actual iPhone (I get the same problem testing with the simulator though)
Around the same time as this problem I also noticed that NSLog function doesn't work inside AppDelegate applicationHasLaunched method
Thanks
You didn't set the action for that button.
Change your createMyButton method like:
- (UIButton*) createMyButton
{
UIButton *button = [[UIButton alloc] init];
[button setTitle: #"My Button"
forState: UIControlStateNormal];
button.frame = CGRectMake(10, 10, 100, 40);
[button addTarget:self action:#selector(myAction:) forControlEvents:UIControlEventTouchUpInside];
return button;
}
Also change myAction method like:
- (void)myAction:(UIButton *)sender
{
[sender performSelectorOnMainThread: #selector(removeFromSuperview) withObject: nil waitUntilDone: NO];
}
Your code seems overly complicated:
Why do you need to lazy create the button?
Why does it need to be readonly?
Why do you need the performselector in your action?
I have taken the liberty to rewrite your code:
Header file
#property (nonatomic,strong) UIButton * button;
Code file
- (void)viewDidLoad
{
[super viewDidLoad];
self.button = [UIButton buttonWithType:UIButtonTypeSystem];
[self.button setTitle:#"MyButton" forState:UIControlStateNormal];
self.button.frame = self.view.bounds;
[self.button addTarget:self action:#selector(myAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.button];
}
-(void)myAction:(UIButton*)sender
{
[sender removeFromSuperview];
}
Happy coding!
Edit
If you insist on going with your original code, try changing
- (id) getMyButton
to
-(UIButton*)myButton
Buttons are a class family and should be created with [UIButton buttonWithType:]
I am sure that your myAction is not get fired. Cause you forget to addTarget
on your createMyButton method just add one more line
[button addTarget:self action:#selector(myAction:) forControlEvents:UIControlEventTouchUpInside];
UPDATE: You've also marked this question as iOS but you are importing OSX headers
#import "NSViewController.h"
#interface MyViewController: NSViewController
you should be inheriting from UIViewController
#import <UIKit/UIKit.h>
#interface MyViewController : UIViewController
Are you creating the view controller via the init code you've posted?
- (id) init
{
if([super initWithNibName: nil bundle: nil])
{
_myButton = nil;
}
return self;
}
Here you are not allocating anything to self the code should be
- (id) init
{
self = [super initWithNibName: nil bundle: nil];
if(self)
{
_myButton = nil;
}
return self;
}

Resources