Using UIButtons to open a different .nib - iphone

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.

Related

Crash in the simulator when i try to go to an other view

so I have a tab bar application where i have put a button to go to an other view.
so in the FirstView.h i have that
IBOutlet ContactMainViewController *contactMainViewController;
and that
-(IBAction)gotoContactMainViewController;
in my FirstView.m i have
-(IBAction)gotoContactMainViewController {
[self presentModalViewController:contactMainViewController animated:YES];
}
and in my SecondView that i have created in the .h
-(IBAction)goBack;
and in the .m
-(IBAction)goBack {
[self dismissModalViewControllerAnimated:YES];
}
The app run correctly but when i click on the button, a green line come over
[self presentModalViewController:contactMainViewController animated:YES];
and say "Thread 1: Program received signal: "SIGABRT"
and this in the console:
terminate called throwing an exception[Switching to process 3908 thread 0xb303]
Thanks for your help !
What kind of FirstView and ContactMainViewController class file? is it a UIViewController or simple UIView?
we assume that ContactMainViewController is a separate class file
in your firstView.m
#import "ContactMainViewController.h"
-(IBAction)gotoContactMainViewController {
ContactMainViewController _contactMainVC = [[ContactMainViewController alloc]init];
[self presentModalViewController:_contactMainVC animated:YES];
[_contactMainVC release];
}
Add the #class ContactMainViewController; in the header before the #interface. That should do it. Also be sure to #import "ContactMainViewController.h" in your .m file to avoid warnings
You must add the code provided by Legolas just before:
[self presentModalViewController:contactMainViewController animated:YES];
You need to allocate contactMainViewController before pushing views.
-(IBAction)gotoContactMainViewController {
if (self.contactMainViewController == nil)
self.contactMainViewController = [[ContactMainViewController alloc] init];
[self presentModalViewController:contactMainViewController animated:YES];
}

Modal View Not Working

I am using the following code in my view controller and I want it to present another view controller called "chooserViewController" modaly
- (void)presentModalViewController:(UIViewController *)modalViewController animated:(BOOL)animated
{
[self presentModalViewController:chooserViewController animated:YES];
}
I am getting a compile error not recognizing "chooserViewController". Am I doing it wrong?
Update:
- (void)add:(id)sender
{
RoutineExerciseChooserViewController *routineExerciseChooserViewController = [[RoutineExerciseChooserViewController alloc] initWithNibName:#"RoutineExerciseChooserViewController" bundle: nil];
[self presentModalViewController:routineExerciseChooserViewController animated:YES];
[routineExerciseChooserViewController release];
}
You need to create chooserViewController:
- (void)presentModalViewController:(UIViewController *)modalViewController animated:(BOOL)animated {
ChooserViewController *chooserViewController = [[ChooserViewController alloc] initWithNibName:#"ChooserView" bundle: nil];
[self presentModalViewController:chooserViewController animated:YES];
[chooserViewController release];
}
If you're not loading from a nib, obviously you'll use a different way to create chooserViewController, but you have to do something to ensure it exists, and can then be presented.

Why is My Application Crashing When I Call a Method in self.parentViewController?

In my code I'm doing the following:
-(void)pushCropImageViewControllerWithDictionary:(NSDictionary *)dictionary {
civc = [[CropImageViewController alloc] init];
[self presentModalViewController:civc animated:YES];
civc.myImage.image = [dictionary objectForKey:UIImagePickerControllerOriginalImage];
}
So I have a modal view in my app. When this modal view dismisses, I want to call a method from the parent view (the view that called pushCropImageViewControllerWithDictionary), like this:
-(void)viewWillDisappear:(BOOL)animated {
[super viewWillAppear:animated];
[(AddNewItemViewController *)self.parentViewController addCroppedPicture:screenshot];
}
But it keeps crashing with the following message:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITabBarController addCroppedPicture:]: unrecognized selector sent to instance 0x4d15930'
Can someone tell me what am I doing wrong? I am including the header for AddNewItemViewController so the selector should be recognized. Can someone give me a hand on how can I do this properly? Thanks.
EDIT: Declaration of addCroppedPicture:
-(void)addCroppedPicture:(UIImage *)image;
The implementation itself is empty so far.
Apparently, self.parentViewController isn't an instance of AddNewItemViewController but the tab bar controller. Hence the crash.
The proper solution is to make a delegate:
-(void)pushCropImageViewControllerWithDictionary:(NSDictionary *)dictionary
{
civc = [[CropImageViewController alloc] init];
civc.delegate = self;
[self presentModalViewController:civc animated:YES];
...
}
To send a message back to the delegate:
-(void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[self.delegate addCroppedPicture:screenshot];
}
It's up to you do declare the delegate protocol:
#protocol CropImageDelegate <NSObject>
- (void)addCroppedPicture:(UIImage*)image;
#end
And add a property for the delegate to CropImageViewController:
#property (nonatomic, assign) id<CropImageDelegate> delegate;
And finally, make your view controller conform to this delegate protocol.

Crash in ABPeoplePicker when called from another modal viewcontroller and both dismissed

(Note: I filed this question before in the context of my project, but I've now recreated the crash in a test project. Any help in telling me what I'm doing wrong would be appreciated.)
The crash occurs when calling ABPeoplePicker from another modal viewcontroller. Specifically, the main window has a NavController, which loads myVC. myVC then loads a modal NavController containing my controller, which then calls ABPeoplePicker. In this demo program, no user intervention is necessary until ABPeoplePicker runs.
The crash occurs if you use the search box in the people picker, and then select one of the resulting people. (If you use the simulator, you'll need to add a person in Contacts before running the program.) The program returns, but during the dismissal of the two modal VCs, I get an assertion error crash. It occurs every time on iphone, ipad, and simulators for both. This seems a very normal thing to do, so I find it hard to believe this is a real bug. The crash message is:
Assertion failure in
-[ABMembersSearchDisplayController setActive:animated:],
/SourceCache/UIKit_Sim/UIKit-1448.69/UISearchDisplayController.m:589 2011-01-31 13:51:11.903
testcrasher2[26044:207] *
Terminating app due to uncaught
exception
'NSInternalInconsistencyException',
reason: 'search contents navigation
controller must not change between
-setActive:YES and -setActive:NO'
So to demonstrate, in a new Xcode iPhone Window application, I modify the didFinishLaunchingWithOptions to call my controller. Then I create two VCs as follows. (Note you need to add Addressbook frameworks to the target.) Here's the entire program...
AppDelegate.didFinishLaunchingWithOptions:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
myViewController *detailViewController = [[myViewController alloc] init];
// Set the navigation controller as the window's root view controller and display.
UINavigationController * navController = [[UINavigationController alloc] initWithRootViewController: detailViewController];
self.window.rootViewController = navController;
[self.window makeKeyAndVisible];
[detailViewController release];
[navController release];
return YES;
}
myViewController.h:
#interface myViewController : UIViewController<addDelegate>{
}
#end
myViewController.m:
#import "myViewController.h"
#import "AddNewViewController.h"
#implementation myViewController
- (void)controllerDidFinish:(addNewViewController *)controller {
[self dismissModalViewControllerAnimated:YES];
}
-(void) viewWillAppear:(BOOL)animated {
[super viewWillAppear: animated];
addNewViewController *addController = [[addNewViewController alloc] init];
addController.delegate = self;
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:addController];
[self presentModalViewController:navController animated:YES];
[navController release];
[addController release];
}
#end
AddNewViewController.h:
#import <AddressBookUI/AddressBookUI.h>
#protocol addDelegate;
#interface addNewViewController : UIViewController < ABPeoplePickerNavigationControllerDelegate> {
id <addDelegate> delegate;
}
#property(nonatomic, assign) id <addDelegate> delegate;
#end
#protocol addDelegate <NSObject>
- (void)controllerDidFinish:(addNewViewController *)controller ;
#end
AddNewViewController.m:
#import "AddNewViewController.h"
#implementation addNewViewController
#synthesize delegate;
-(void) viewDidAppear:(BOOL)animated {
ABPeoplePickerNavigationController * peoplepicker = [[ABPeoplePickerNavigationController alloc] init] ;
peoplepicker.peoplePickerDelegate = self;
[self presentModalViewController:peoplepicker animated:YES];
[peoplepicker release];
}
#pragma mark AddressBook delegate methods
- (void)peoplePickerNavigationControllerDidCancel: (ABPeoplePickerNavigationController *)peoplePicker {
[self dismissModalViewControllerAnimated:YES];
}
- (BOOL)peoplePickerNavigationController: (ABPeoplePickerNavigationController *)peoplePicker
shouldContinueAfterSelectingPerson:(ABRecordRef)person {
[self.delegate controllerDidFinish:self ];
return NO; //EDIT: This MUST be YES or it will crash (see answer below)
}
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker
shouldContinueAfterSelectingPerson:(ABRecordRef)person
property:(ABPropertyID)property
identifier:(ABMultiValueIdentifier)identifier {
return NO;
}
#end
Turns out this is an actual bug. It is indeed true that if you do a double ModalVC dismiss to ABPeoplePicker when the user clicks a person in search, you'll get this crash. Fortunately, there's a simple workaround: return YES in your delegate's shouldContinueAfterSelectingPerson. As you're simultaneously dismissing the picker, it doesn't really matter whether you return YES or NO, it won't continue, but NO will crash and YES doesn't. (Same answer as for my original post: Weird crash in ABPeoplePicker )
The bug is in fact in your code. Took me a few minutes to find it, I'll try to explain as best I can.
Your
ABPeoplePickerNavigationController
is currently presented modally.
You click in the search bar and type
some stuff.
You click a person's name.
What happens here, is the ABPeoplePickerNavigationController asks its delegate (which is your addNewViewController) whether it should continue after selecting a person. While it's waiting to hear back from you, you suddenly call your own protocol's method (in myViewController) that attempts to dismiss the modal addNewViewController. You're jumping ahead of yourself, as the ABPeoplePickerNavigationController is still open.
Change your implementation of the ABPeoplePickerNavigationControllerDelegate method to read:
- (BOOL)peoplePickerNavigationController: (ABPeoplePickerNavigationController *)peoplePicker
shouldContinueAfterSelectingPerson:(ABRecordRef)person {
// This line is new.
[self.navigationController dismissModalViewControllerAnimated:YES];
[self.delegate controllerDidFinish:self];
return NO;
}
And your crash will go away. When you're dealing with layers upon layers of UIViewControllers and UINavigationControllers, you have to be very careful to dismiss them in the reverse order you presented them.

NSInvalidArgumentException

I Am creating an iPhone application which displays an UITableView at the start with some text in the cells. When a user taps a cell it should transition to another view. When I run the application in the iPhone Simulator and click on a cell I get the following error:
2009-09-23 12:20:03.554 ZDFMobiel[751:20b] *** Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: '*** -[RootViewController eigenRisicoView]:
unrecognized selector sent to instance 0xd1d1a0'
2009-09-23 12:20:03.555 ZDFMobiel[751:20b] Stack: (
Here is the function (In RootViewController.m) where the error occurred. It happens at the
if(self.eigenRisicoView == nil) {
line or the at the other if statements, depending on which cell you click.
// Override to support row selection in the table view.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
ZDFMobielAppDelegate *appDelegate = (ZDFMobielAppDelegate *)[[UIApplication sharedApplication] delegate];
NSString *content = (NSString *)[appDelegate.menuItems objectAtIndex:indexPath.row];
switch (indexPath.row) {
case 0:
if(self.eigenRisicoView == nil) {
EigenRisicoViewController *viewController = [[EigenRisicoViewController alloc] initWithNibName:#"EigenRisicoViewController" bundle:[NSBundle mainBundle]];
self.eigenRisicoView = viewController;
[viewController release];
}
[self.navigationController pushViewController:self.eigenRisicoView animated:YES];
self.eigenRisicoView.title = content;
break;
case 1:
if(self.declaratieStatusView == nil) {
DeclaratieStatusViewController *viewController = [[DeclaratieStatusViewController alloc] initWithNibName:#"DeclaratieStatusViewController" bundle:[NSBundle mainBundle]];
self.declaratieStatusView = viewController;
[viewController release];
}
[self.navigationController pushViewController:self.declaratieStatusView animated:YES];
self.declaratieStatusView.title = content;
break;
case 2:
if(self.vergoedingenView == nil) {
VergoedingenViewController *viewController = [[VergoedingenViewController alloc] initWithNibName:#"VergoedingenViewController" bundle:[NSBundle mainBundle]];
self.vergoedingenView = viewController;
[viewController release];
}
[self.navigationController pushViewController:self.vergoedingenView animated:YES];
self.vergoedingenView.title = content;
break;
case 3:
if(self.zoekenZorgInstView == nil) {
ZoekenZorgInstViewController *viewController = [[ZoekenZorgInstViewController alloc] initWithNibName:#"ZoekenZorgInstViewController" bundle:[NSBundle mainBundle]];
self.zoekenZorgInstView = viewController;
[viewController release];
}
[self.navigationController pushViewController:self.zoekenZorgInstView animated:YES];
self.zoekenZorgInstView.title = content;
break;
default:
break;
}
// Navigation logic may go here -- for example, create and push another view controller.
// AnotherViewController *anotherViewController = [[AnotherViewController alloc] initWithNibName:#"AnotherView" bundle:nil];
// [self.navigationController pushViewController:anotherViewController animated:YES];
// [anotherViewController release];
}
This is my RootControllerView.h file
#import "EigenRisicoViewController.h";
#import "DeclaratieStatusViewController.h";
#import "VergoedingenViewController.h";
#import "ZoekenZorgInstViewController.h";
#import "ZDFMobielAppDelegate.h";
#interface RootViewController : UITableViewController {
EigenRisicoViewController *eigenRisicoView;
DeclaratieStatusViewController *declaratieStatusView;
VergoedingenViewController *vergoedingenView;
ZoekenZorgInstViewController *zoekenZorgInstView;
}
#property(nonatomic,retain) EigenRisicoViewController *eigenRisicoView;
#property(nonatomic,retain) DeclaratieStatusViewController *declaratieStatusView;
#property(nonatomic,retain) VergoedingenViewController *vergoedingenView;
#property(nonatomic,retain) ZoekenZorgInstViewController *zoekenZorgInstView;
#end
I Am new to iPhone development, Objective C and XCode so I don't understand what the error means..
Does anyone know what I am doing wrong?
I'd need to see the whole implementation, but it sounds like you didnt #synthesize eigenRisicoView;