0

I'm getting my feet wet with objective-C. And thought I'd play with iphone development. I have a Page Controller.h and .m

Inside my implementation file I've thrown a UIButton inside the viewDidLoad method.

- (void)viewDidLoad { [super viewDidLoad]; // content that loads in view UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; myButton.frame = CGRectMake(10, 160, 300, 44); [myButton setTitle:@"Press Me!" forState:UIControlStateNormal]; myButton.titleLabel.font = [UIFont systemFontOfSize:14]; // add to a view [self.view addSubview:myButton]; } 

So far it's showing fine. This makes me ask about the difference between loadView vs viewDidLoad.

Also, I want to run my own function when the button is pressed, but I constantly get an executable error from xcode.

Here's what I've interjected within the code above:

- (void)viewDidLoad { [super viewDidLoad]; // content that loads in view UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; myButton.frame = CGRectMake(10, 160, 300, 44); [myButton setTitle:@"Press Me!" forState:UIControlStateNormal]; myButton.titleLabel.font = [UIFont systemFontOfSize:14]; //added this [myButton addTarget:self action:@selector(runClick) forControlEvents:UIControlEventTouchUpInside]; // add to a view [self.view addSubview:myButton]; } -(void)runClick{ NSLog(@"does this work?"); } 

Does anything seem out of place to anyone?

Also, In the PageController.h I added: -(void)runClick;

Here's the Error I catch:

2012-03-17 16:04:30.077 Blah[510:207] -[__NSCFString runClick]: unrecognized selector sent to instance 0x6a0b4f0

2
  • what's that executable error? Commented Mar 17, 2012 at 22:02
  • this: 2012-03-17 16:04:30.077 Blah[510:207] -[__NSCFString runClick]: unrecognized selector sent to instance 0x6a0b4f0 Commented Mar 17, 2012 at 22:05

3 Answers 3

1

About the difference between loadView and viewDidLoad:

  • According to Apple, the controller (not you!) calls this method when it's view is requested and it's nil. So, it will create and load the view.
  • You can override loadView to create your own view (if you don't have a nib for that controller)
  • If you want to add something to the view created by a view controller with a nib file do it on viewDidLoad, and remove it on viewDidUnload.

About the error, can you provide more information? Are you debugging? What's the line it crashes?

Sign up to request clarification or add additional context in comments.

Comments

1

Did you declare -(void)runClick in your .h file? (obvious question).

Also, if you want to get access to the control's properties (say if it were a slider, etc.), you need to add the sender bit:

-(void)runClick (id)sender

Comments

0

I believe you need to add a colon after runClick as in:

action:@selector(runClick:) 

1 Comment

the : only needs if selector accepts the argument. So in this question case the function - (void) runClick { } no needs to call it using :.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.