Unrecognized selector sent - ios

I have 1 View Controller and 1 Tab Bar Controller with 2 Navegation Controller.
In my simple View I have one form and then when I click in button "Login" I want to load the first View in my Tab Bar Controller.
With this code works perfect:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
Inicial *telaInicial = [storyboard instantiateViewControllerWithIdentifier:#"tabBar"];
[self presentViewController:telaInicial animated:YES completion:nil];
But I want to send and value to this View "Inicial" and I put this:
telaInicial.email = email.text;
But I got this error message:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITabBarController setEmail:]: unrecognized selector sent to instance 0x9fdc8f0'

The object telaInicial that you think is an instance of Inicial is actually a UITabBarController therefore you cannot set the email property on it as it doesn't exist.
I think you want something like this...
UITabBarController *tabBarController = [self.storyboard instantiateViewControllerWithIdentifier:#"tabBar"];
UINavigationController *navController = tabBarController.viewControllers[0];
Inicial *telaInicial = navController.topViewController;
telaInicial.email = // blah

It means that there isn't a setEmail method for the telaInicial object. Is that an instance of a subclass you've written? If so, you might be getting the error because you haven't written a setEmail method for it or because you haven't declared the instance variable email as an #property, in which case the setter and getter methods for it would be synthesized automatically by XCode (prior to XCode 4.something, I think you need to synthesize your properties explicitly - after that version, they're automatic).

Try to write class, that will store your data.
You can access to it from AppDelegate (not really good solution, but very fast).

Related

MainViewController pushViewController:animated:]: unrecognized selector sent to instance 0x1459e980

I'm trying to add a functionality to open a specific view controller on opening the notification on my iOS app. As I am new to iOS, I have a few doubts.
I have written the backend, which sends the promo id as a custom parameter. I am able receive the JSON object on XCode, read the ID as a NSString.
Now, I want to open the PromotionDetailViewController with the promotion id as the parameter.
What I have done up to now is,
PromotionDetailViewController *pVC = [[PromotionDetailViewController alloc] initWithPromotionID:promo_id[#"promo_id"]];
[(UINavigationController *)self.window.rootViewController pushViewController:pVC animated:YES];
[(UINavigationController *)[UIApplication sharedApplication].keyWindow.rootViewController popViewControllerAnimated:YES ];
I am able to receive and read the promo ID in the AppDelegate.m file.
When I run the above code, I get the below error.
MainViewController pushViewController:animated:]: unrecognized
selector sent to instance 0x1459e980
Any help in the right direction is great! I do php and android, but Obj-C is quite different!
UPDATE
Changed the code to this:
PromotionDetailViewController *pvc = [[PromotionDetailViewController alloc] initWithPromotionID:promo_id[#"promo_id"]];
UINavigationController *promoNav =
[[UINavigationController alloc] initWithRootViewController:pvc];
self.window.rootViewController = promoNav;
Now the API call is successful, I am able to see the correct NAV bar, but the app crashes due to the below error:
Terminating app due to uncaught exception 'NSRangeException', reason:
'*** -[__NSArrayI objectAtIndex:]: index 0 beyond bounds for empty
array'
(UINavigationController *)self.window.rootViewController it seems that is not a UINavigationController.
To get the correct NavigationController you can do self.navigationController if 'self' is already a child controller of the navigation one.
In obj-c Unrecognized selector means that you are sending a message to an object that does not respond to that, since you are casting it manually with parenthesis, the compiler doesn't complain and thinks is actually a NavgiationController, but isn't.

crash passing parameter from one viewcontroller to another

I'm trying to pass parameter from one view controller to another, but application crashes with exception.
My code is this:
SelectedItemViewController *nextView = [storyboard instantiateViewControllerWithIdentifier:#"SelectedItemViewID"];
nextView.m_selectedItemId = [NSNumber numberWithInt:777];
[self presentViewController:nextView animated:YES completion:NULL];
And I have the following in my stack:
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UINavigationController setM_selectedItemId:]: unrecognized selector sent to instance 0x9c765d0'
In your storyboard, the view controller with identifier SelectedItemViewID is a navigation controller, not a SelectedItemViewController. So, before you set m_selectedItemId you should access the navigation controllers root view controller (which should be your SelectedItemViewController).
I found solution here: instantiateViewControllerWithIdentifier and pass data
What should be done in case if you want to pass parameter to view controller via navigation controller:
UINavigationController *navigationController = [storyboard instantiateViewControllerWithIdentifier:#"SelectedItemViewID"];
SelectedItemViewController *nextView = navigationController.viewControllers[0];
nextView.m_selectedItemId = [NSNumber numberWithInt:777];
[self presentViewController:navigationController animated:YES completion:NULL];
When you select the view controller in your storyboard, is the class set to SelectedItemViewController?
Obviously you seem to have a #property ... m_selectedItemId (or at least setter method setM_selectedItemId) on SelectedItemViewController, otherwise your code wouldn't even compile. But it seems that [storyboard instantiateViewControllerWithIdentifier:#"SelectedItemViewID"] doesn't really return the expected SelectedItemViewController. Hence I have the feeling that you forgot to set the correct class name to SelectedItemViewController in your storyboard.

Segue from segmentcontrol in XIB Viewcontroller to Storyboard Viewcontroller

I have a segment control in a viewcontroller (UISegmentController.h, UISegmentController.m, UISegmentController.xib) created in a xib. Now I am trying to segue (ideally push) to Storyboard viewcontroller (ChartviewController.h, ChartviewController.m) I created. This Viewcontroller will hold images. I tried to perform this push as below:
-(IBAction) segmentedControlIndexChanged;
{
//some code
ChartViewController *chartviewpage = [[ChartViewController alloc] initWithNibName:#"ChartViewController" bundle:nil];
[self.navigationController pushViewController:chartviewpage animated:YES];
//some code
}
I used to implement this code to another xib viewcontroller and it worked fine. But now not with the stroryboard viewcontroller.
However, I receive an exception as follows:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle (loaded)' with name 'ChartViewController''
* First throw call stack:
(0x15e6012 0x11b3e7e 0x15e5deb 0x53ffac 0x404e37 0x405418 0x405648 0x405882 0x405b2a 0x41cef5 0x41cfdb 0x41d286 0x41d381 0x41deab 0x41e4a3 0x41e098 0x4048 0x11c7705 0x327920 0x3278b8 0x3e8671 0x3e8bcf 0x3e86a6 0x43d597 0x43f83b 0x35716d 0x357552 0x3353aa 0x326cf8 0x22d1df9 0x22d1ad0 0x155bbf5 0x155b962 0x158cbb6 0x158bf44 0x158be1b 0x22d07e3 0x22d0668 0x32465c 0x1d0d 0x1c35)
libc++abi.dylib: terminate called throwing an exception
(lldb)
Can u help please?
Thanks very much!
I finally figured it out doing a bit more research. first, one needs to instantiate the controller (ChartViewcontroller) in the storyboard they are trying to transition to. Then you need to give a storyboard ID name (ChartID) to the controller which can be find under the file inspector after selecting the controller. Finally perform the push to the new controller.
Hope this helps someone.
UIStoryboard * myStoryboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
ChartViewController *chartpage = [myStoryboard instantiateViewControllerWithIdentifier:#"ChartID"];
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:chartpage animated:YES];

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Application tried to present a nil modal view controller on target

I'm new in iphone, I'm trying to open a view when pressing a button,
in my class "ReaderViewController" I wrote the following code:
- (void)tappedInToolbar:(ReaderMainToolbar *)toolbar emailButton:(UIButton *)button
{
#ifdef DEBUGX
NSLog(#"%s", __FUNCTION__);
#endif
#if (READER_ENABLE_MAIL == TRUE) // Option
instantiateViewControllerWithIdentifier:#"searchView"];
if (printInteraction != nil) [printInteraction dismissAnimated:NO]; // Dismiss
SearchViewController *searchController = [self.storyboard instantiateViewControllerWithIdentifier:#"searchView"];
searchController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
searchController.modalPresentationStyle = UIModalPresentationFullScreen;
[self presentModalViewController:searchController animated:YES];
[searchController release];
#endif // end of READER_ENABLE_MAIL Option
}
I want when to open the view of this controller "SearchViewController" and in storyboard I gave the view of SearchViewController its identifier name "searchView"
but when I run, it gives me the following Exception:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Application tried to present a nil modal view controller on target <ReaderViewController: 0x6864930>.'
Any Help ??
Thanks in advanced.
There's no view controller with the identifier searchView in your storyboard, so the instantiation fails and the
instantiateViewControllerWithIdentifier:
method returns nil. Double check your Storyboard setup and also note that identifiers and names are generally case-sensitive, maybe you named your view controller SearchView and not searchView.
It also may be the case that
self.storyboard
itself isn't properly initialized or instantiated, and thus is nil.
Edit: so you were creating the storyboard from code, but in fact you weren't. The solution was to manually instantiate UIStoryboard.
As for your own comment, you'd better assign self.storyboard = Storyboard or you'll continue getting these errors...
It looks to me like self.storyboard is nil. This means every message you are passing to it results in nil also.

iOS - NavigationController thinks it's getting the same controller pushed twice

I'm a bit confused about the following code. If I comment out the second statement, it successfully shows the view:
MyAppDelegate *delegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
delegate.navController.viewControllers = [NSArray arrayWithObject:aViewController];
[delegate.navController pushViewController:aViewController animated:YES];
[aViewController release];
Otherwise, it crashes on the following:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Pushing the same view controller instance more than once is not supported
I add a different view controller in the app delegate, but not this one. What could be making it think it's the same one?
delegate.navController.viewControllers = [NSArray arrayWithObject:aViewController];
[delegate.navController pushViewController:aViewController animated:YES];
First line set's aViewController as navController's only controller. Second line pushes aViewController to navController again so yeah, no wonder you get it twice. Depending on what you want to do, ditch one of those two lines.
If you want to set aViewController as only controller on navController, keep first line.
If you want to push aViewController as a new controller on navController, keep second line.

Resources