unrecognized selector sent to instance when trying to open new viewController - iphone

I've got 2 viewControllers:
DemoAppViewController
AboutViewController
I have an info button in DemoAppViewController which is supposed to open AboutViewController.
At the moment I'm getting a run time error and i can't seem to work out why..
Here's the code:
DemoAppViewController .h:
- (IBAction)showInfo;
DemoAppViewController.m:
- (IBAction)showInfo {
AboutViewController *controller = [[AboutViewController alloc] initWithNibName:#"AboutViewController" bundle:nil];
//setting style with modalTransition property
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
//show on screen
[self presentViewController:controller animated:YES completion:nil];
}
When i create the AboutViewController object with initWithNibName xCode adds the following code to AboutViewController.m:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
I've left the above untouched.
Runtime error:
[DemoAppViewController showInfo:]: unrecognized selector sent to instance 0x688e450
Can someone tell me where I'm going wrong?
Thanks.

The selector for this method is #selector(showInfo), not #selector(showInfo:). If you assign it programmatically, just correct it. If you use IB - add :(id) sender parameter to the method.

Related

viewDidLoad not Called for UIViewController with UIScrollView and UIPageControl

I am trying to add a page with a UIScrollView and UIPageControl to my app. I am making the call to the new class from a UITableView as below.
MultiPageViewController *mpvc = [[MultiPageViewController alloc]initWithNibName:#"MultiPageViewController" bundle:[NSBundle mainBundle]];
[[self navigationController]pushViewController:mpvc animated:YES];
The viewDidLoad function is never called, thus my UIScrollview and UIPageControl are never initialized. When I go along further and call viewWillAppear these objects are both still nil. Further the view never appears only a white screen. Below is the code from initWithNibName and viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
NSLog(#"initWithNibName returned self != null");
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(#"View did load called");
}
Thanks for your help in advance.

Using UIButtons to open a different .nib

something isn't working right and i can't work out why this isn't working to load my other nib, this currently works
#pragma mark - Flipside View
- (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller
{
[self dismissModalViewControllerAnimated:YES];
}
- (IBAction)showInfo:(id)sender
{
FlipsideViewController *controller = [[FlipsideViewController alloc] initWithNibName:#"FlipsideViewController" bundle:nil];
controller.delegate = self;
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:controller animated:YES];
}
whilst i've done the exact same thing here on the same MainViewController.m and had no success whilst doing it
#pragma mark - News View
- (void)newsViewControllerDidFinish:(NewsViewController *)controller
{
[self dismissModalViewControllerAnimated:YES];
}
- (IBAction)showNews:(id)sender
{
NewsViewController *controller = [[NewsViewController alloc] initWithNibName:#"NewsViewController" bundle:nil];
controller.delegate = self;
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:controller animated:YES];
}
so i think there is something wrong with my header file which looks like this
#import "FlipsideViewController.h"
#import "NewsViewController.h"
#interface MainViewController : UIViewController <FlipsideViewControllerDelegate, NewsViewControllerDelegate>
- (IBAction)showInfo:(id)sender;
- (IBAction)showNews:(id)sender;
#end
I can't work out why it's not working, any help would be appreciated.
Error from output:
This GDB was configured as "x86_64-apple-darwin".Attaching to
process 2147.
2011-07-08 12:24:09.845 Danny[2147:ef03] -[NewsViewController
setDelegate:]: unrecognized selector
sent to instance 0x68a62b0
2011-07-08 12:24:09.847 Danny[2147:ef03] * Terminating app
due to uncaught exception
'NSInvalidArgumentException', reason:
'-[NewsViewController setDelegate:]:
unrecognized selector sent to instance
0x68a62b0'
* First throw call stack:
(0xf8a600 0x112252e 0xf8d550 0xeeb4cf 0xeeb292 0x2a36 0xf8bd78
0x18cc5 0x18c5a 0xbdbd4 0xbe09d
0xbd368 0x3e004 0x3e22d 0x24990
0x181a7 0x1369886 0xf59d11 0xebbc9b
0xeba4b1 0xeb993c 0xeb9868 0x1367fef
0x13680b4 0x160c4 0x2009 0x1f75)
terminate called throwing an exceptionsharedlibrary
apply-load-rules all
Current language: auto; currently objective-c
(gdb)
What is the delegate method for the view controller? Check if the declaration of the delegate method is the same. Make sure you have set the delegates properly
Try this:
- (IBAction)showNews:(id)sender
{
NewsViewController *controller = [[NewsViewController alloc] initWithNibName:#"NewsViewController" bundle:nil];
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:controller animated:YES];
}
And in the News View, instead of writing
[self.delegate newsViewControllerDidFinish:self];
you should write:
[self dismissModalViewControllerAnimated:YES];
That should solve your problem. I don't see much point in writing extra delegates, they don't usually give you any advantages (in scenarios like this one), when you can just have a view controller dismiss itself.

Extending UIVIewController of my own I got error accessing self.view

I have created a View controller with some methods I need to use often in different aplications. It works like a charm if I use it directly but when I try to create another UIViewController that extend my class I cannot access self.view anymore. This is the init method of the original class:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil slideFrom:(HalfViewControllerType) from {
self.slideFrom = from;
if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
CGRect viewScreen = [self.view bounds];
[self moveTo:startPoint inDuration:0.0];
}
return self;
}
At the point of retrieving [self.view bounds] I got an EXC_BAD_ACCESS.
Even hacking the values manually it then fail to all the other self.view like transform animation and so on.
The call to create the view is this, but it never got after the init method:
SelectViewController *sVC = [[SelectViewController alloc] initWithNibName:#"SelectViewController" bundle:nil slideFrom:top];
[sVC setDelegate:self];
[self.view addSubview:sVC.view];
[sVC slideIn];
[sVC release];
Any help on understanding what I am doing wrong would be really appreciated.
Francesco.
I couldn't understand what was happening, so I re-wrote everything and now works. I can just guess I was releasing something I shouldn't have.

Customising UITabBarItems in their controllers?

In previous applications I have customised my tabBarItems by overriding init (see below)
- (id)init {
self = [super init];
if(self) {
UITabBarItem *tabBarItem = [self tabBarItem];
[tabBarItem setTitle:#"ONE"];
}
return self;
}
After looking at the Xcode templates I am now thinking that I would be better to add this customisation to initWithNibName:bundle: instead.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
UITabBarItem *tabBarItem = [self tabBarItem];
[tabBarItem setTitle:#"ONE"];
}
return self;
}
does this make sense, it seems like it does to me, but I just wanted to check?
Gary
It depends on whether you load your controller from a Nib (xib) file or not (and so you do all the work programmatically in the init) I guess

iphone 2.x sdk - reference to a viewController inside another viewController showing up as null

I'm trying to give a newly created instance of a custom picker view controller a reference to another viewController like so (this is inside of a selector from a ponceViewController instance that is called after a tableView row is tapped)...
- (IBAction)rowTapped:(id)sender {
TimerPickerViewController *viewController = [[TimerPickerViewController alloc] initWithNibName:#"TimerPickerView" bundle:nil]
self.timerPickerViewController = viewController;
timerPickerViewController.ponceViewController = self.rootViewController;
[viewController release];
}
Then inside my timerPickerViewController instance I have:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
...
// ponceViewController is null here
...
}
}
The timerPickerViewController displays just fine, and I can even access stuff from ponceViewController after I tap my "Done" button, but I'm synthesizing ponceViewController and it's in my header and everything, and I can't seem to access it inside of timerPickerViewController's initWithNibName method - it's always null. :( Anyone have any ideas?
Edit: I should also mention that ponceViewController is null inside timerPickerViewController's viewDidLoad method as well...
- (void)viewDidLoad {
... no such thing as ponceViewController here! ...
}
So I fixed it by completely removing initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil from timerPickerViewController and used viewDidLoad instead. Everything seems to work fine. I think the issue was that the property wasn't available yet when referencing it inside initWithNibName. Hope that helps someone else.