PDA

View Full Version : Cannot connect UIButton event in Xcode 4




tutiplain
Apr 10, 2011, 11:44 AM
Hi all,

Quick question: has the way to connect an event to an UIButton changed in Xcode 4 and the latest version of the SDK? I have a project made in the previous version of Xcode with an UIButton connected to an event in Interface Builder, and the event executes correctly. The event is declared as follows:


-(IBAction)showNextView
{
NSLog(@"Event executed");
}


The event code resides in the application delegate class, and it is connected in IB directly on the UIButton's Touch Down event. There is no @property declaration for the UIButton, yet the event executes. I have another project created in Xcode 4 set up exactly the same way, yet the event throws an exception when executed, something about an "Invalid selector" (I don't have the exact message with me right now). I also get a similar exception if I try to create a @property for the UIButton and connect it in IB. Does anyone have idea why this happens? I will try and post my project files later. Thanks for any info.



chown33
Apr 10, 2011, 12:27 PM
Post the exact error message. An "invalid selector" error usually shows the exact selector expected.

tutiplain
Apr 10, 2011, 09:50 PM
Hi again,

The exact error message is


2011-04-10 22:49:45.657 ForoTest[219:207] -[UIViewController showNextView]: unrecognized selector sent to instance 0x4b38630
2011-04-10 22:49:45.735 ForoTest[219:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIViewController showNextView]: unrecognized selector sent to instance 0x4b38630'


I have attached the project files to the thread. Any help will be welcome. Thanks.

chown33
Apr 11, 2011, 12:09 AM
Without looking at the code, the error message should tell you what's wrong. I've hilited the significant part in bold:


2011-04-10 22:49:45.657 ForoTest[219:207] -[UIViewController showNextView]: unrecognized selector sent to instance 0x4b38630
2011-04-10 22:49:45.735 ForoTest[219:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIViewController showNextView]: unrecognized selector sent to instance 0x4b38630'


It's sending a showNextView message to a UIViewController instance.

Your first post said the method was in your app delegate, not a UIViewController, so check your source first. When I look at the source, I see showNextView in ForoTestViewController, not the app delegate like you said.

I also advise you to check your classes in the nib, to make sure a ForoTestViewController is being instantiated.

It's unclear how you made the non-working Xcode 4 project. If you started with the known-working project, then you probably just broke a connection or misconnected something. If you recreated it entirely from scratch, then there's no easy way of knowing what mistakes you might have made. I'm pretty sure Xcode 4 can open Xcode 3 projects, so I don't know why you'd want to start over.

tutiplain
Apr 11, 2011, 01:37 PM
Hi again,

Actually, I was wrong. I should've said that the method is in the UIViewController subclass (otherwise I would not have been able to connect it in IB, I believe). I have also checked my NIB file ForoTestView.xib, and I am setting the File's Owner class to "ForoTestViewController", so I don't get why the app is sending the "showNextView" message to a UIViewController instance. I believe the problem lies in the way I set up the navigation bar controller. I replaced the template line which assigns the rootViewController for the app. Let me check everything over again, and I'll post back. In the meantime, if you have any suggestions, they are more than welcome.

Thanks again for your time.

chown33
Apr 11, 2011, 01:47 PM
One suggestion: open the known-working Xcode 3 project in Xcode 4. Build it. Make sure it works.

If it works, then use it as a known-good reference to check the non-working project.

If it doesn't work, then figure out why it works in Xcode 3 but not in 4. It may be an Xcode problem, or it may be a faulty assumption in the project that just happens to work in 3 but not in 4.

tutiplain
Apr 11, 2011, 10:54 PM
Hi again,

I managed to solve the problem. My UIViewController subclass as configured correctly. The problem was in the configuration of the Navigation Controller. I had to Specify the Class name in IB for the UIViewController embedded in the Navigation Controller. Thanks for pointing me in the right direction.