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

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.

Related

IOS. Unable to close view after clicking my Info button (newbie)

I have found some code for my infoButton which shows up my new credits.xib but I can't manage to come back to my RootViewController.
On my Credits.xib, I have linked my "Done" button with ToucheDown-FirstResponder-ToggleCredits Close.
Here is my code for the infoButton in my RootViewController.m in ViewDidLoad
UIButton *button = [UIButton buttonWithType:UIButtonTypeInfoLight];
[button addTarget:self action:#selector(toggleCreditsOpen:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *modalButton = [[UIBarButtonItem alloc] initWithCustomView:button];
[self.navigationItem setRightBarButtonItem:modalButton animated:YES];
//[button release];
[modalButton release];
and my code just after my ViewDidLoad
- (IBAction) toggleCreditsOpen:(id)inSender
{
UIViewController *theController = [[UIViewController alloc] initWithNibName:#"Credits" bundle:nil];
[self.navigationController presentModalViewController:theController animated:YES];
}
- (IBAction) toggleCreditsClosed:(id)inSender
{
NSLog(#"Button Pressed!");
//[self.navigationController dismissModalViewControllerAnimated:YES];
[self.parentViewController dismissModalViewControllerAnimated:YES];
}
I think I am missing something should I create a Credits.h and put the toggleCreditsClosed in it ?
Here is the stack trace
*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<UIViewController 0x7c67610> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key DoneButton.'
*** First throw call stack:
Here is my Credits.h
#import <UIKit/UIKit.h>
#interface Credits : UIViewController
{
IBOutlet UIButton *DoneButton;
}
#property (nonatomic, retain) UIButton *DoneButton;
#end
and my Credits.m
#import "Credits.h"
#implementation Credits
#synthesize DoneButton;
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)viewDidUnload
{
}
- (IBAction) toggleCreditsClosed:(id)inSender
{
NSLog(#"Button Pressed!");
[self dismissModalViewControllerAnimated:YES];
}
#end
If a delete the DoneButton link, the Credits view shows up but I it's when I press the Done button that I have a problem
Costumes[402:11f03] -[UIViewController toggleCreditsClosed:]: unrecognized selector sent to instance 0x7a4c460
2012-10-24 22:19:33.271 Costumes[402:11f03] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIViewController toggleCreditsClosed:]:
Sorry but I don't understand what I have to do and I cannot upload an image to show you but
in Outlets the link is (view<->View) and in Received Actions (toggleCreditsClosed:<->Button-Done Touch Down)
Welcome on SO !
Yes, you should create a separate .h / .m for your Credits. Then tell Inteface Builder that your .xib is a Credits class. Then link your button to this .h with the action you want. Basically, your last method should be in Credits.m :
- (IBAction) toggleCreditsClosed:(id)inSender
{
NSLog(#"Button Pressed!");
[self dismissModalViewControllerAnimated:YES];
}
Watch out, you dismiss the modal view with self instead of self.parentViewController in your code !
(PS : answers you'll get may not always work. Don't hesitate to comment, telling us what (didn't) work !)

"EXC_BAD_ACCESS" when switch too fast between an tableView and a mapView

I have a tableView with an button to push a mapView. The push and back actions generally work fine. If I switch quickly between these two views, "EXC_BAD_ACCESS" error will appear.
MapViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
self.mapView.delegate = self;
UIButton *btnL = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40.0, 40.0)];
[btnL setImage:[UIImage imageNamed:#"back.png"] forState:UIControlStateNormal];
[btnL addTarget:self.navigationController action:#selector(popViewControllerAnimated:) forControlEvents:UIControlEventTouchDown];
self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:btnL] autorelease];
[btnL release];
self.whereAmIAnnotation = [[[WhereAmIAnnotation alloc] init] autorelease];
if (!self.mapView || !self.whereAmIAnnotation) {
NSLog(#"mapview : %#", self.mapView);
NSLog(#"whereAmIAnnotation : %#",self.whereAmIAnnotation);
// will never enter in to here
}
[self.mapView addAnnotation:self.whereAmIAnnotation];
}
If I comment [self.mapView addAnnotation:self.whereAmIAnnotation]; , there is no "EXC_BAD_ACCESS" anymore.
Any answers and comments will be appreciated. Thanks in advance!
Edit 2
main.m
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
int main(int argc, char *argv[])
{
#autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
// "EXC_BAD_ACCESS" error message shows here
}
}
Edit 3:
the whereAmIAnnotation is declared here:
MapViewController.m
#interface AddrCoordinateAdjustMapViewController()
#property (retain) WhereAmIAnnotation *whereAmIAnnotation;
#end
#implementation MapViewController
#synthesize whereAmIAnnotation;
Edit 4:
Error message is as following:
2012-07-30 15:56:19.735 myApp[13584:707] *** -[MapViewController respondsToSelector:]: message sent to deallocated instance 0x10195e80
It usually crashes when i switch back to the tableView while the annotationView is dropping.
Have you removed yourself as delegate of the mapview?
self.mapview.delegate = nil;
try removing it at viewWilldisappear or whenever you consider necessary as you might need it later if you have pushed a view controller and will return to the view later.
In a nutshell, remove it when your view cannot respond to the delegate which is why you got the EXC_BAD_ACCSS. it sent a message to your view but it was already released from memory.
Do you release and set to nil all your IBOutlets in the viewDidUnload? So at least you should do:
- (void)viewDidUnload {
self.mapView = nil;
[super viewDidUnload];
}
Generally in ViewDidUnload you should release and set to nil, any view objects that are created from a Nib file or allocated in your ViewDidLoad method
I would suggest to check whether self.whereAmIAnnotation isnt already released by at the time you add it to mapView. That might be one reason for the BAD_ACCESS you receive.
I had similar problem and my solution was to remove all overlays from the map before popping controller from UINavigationController. I was using OpenStreetMap.

iOS AddressBookUI - 'Shouldn't be trying to show more than one Add to Existing Contact people picker.'

I'm trying add some addressbook functionality to my app, but after tap the second time to the 'Add to existing' button I get this error message.
* Assertion failure in -[ABPersonViewControllerHelper presentPeoplePickerNavigationControllerForAddToContacts:], /SourceCache/AddressBookUI_Sim/AddressBookUI-1118/ABPersonViewControllerHelper.m:2574
2011-10-24 20:41:11.960 locato[4576:11603]
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Shouldn't be trying to show more than one Add to Existing Contact people picker.'
First throw call stack:
(0x152e052 0x16bfd0a 0x14d6a78 0xefd2db 0x9d674 0x152fe72 0xd0eb8 0xc8141 0xb9e2f 0x60471d 0x604952 0xe8c86d 0x1502966 0x1502407 0x14657c0 0x1464db4 0x1464ccb 0x17eb879 0x17eb93e 0x574a9b 0x22d9 0x2255)
terminate called throwing an exception
I have a viewcontroller and a navigationcontroller, and im displaying the addressbook's 'modal' views on the main viewcontroller. (or else im not getting the sliding transition, and this error.). I have overridden these methods:
#implementation UIViewController (cancelButton)
- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker;
{
UIResponder *responder = self;
while (responder && ![responder isKindOfClass:[mainViewController class]]) {
responder = [responder nextResponder];
}
[(UIViewController *)responder dismissModalViewControllerAnimated:YES];
}
#end
#implementation UINavigationController (modal)
- (void) presentModalViewController:(UIViewController *)screen animated:(BOOL)animated
{
UIResponder *responder = self;
while (responder && ![responder isKindOfClass:[mainViewController class]]) {
responder = [responder nextResponder];
}
if ([screen isKindOfClass:[ABPeoplePickerNavigationController class]]) {
ABPeoplePickerNavigationController *scr = screen;
scr.peoplePickerDelegate = self;
[scr release];
}
[(UIViewController *)responder presentModalViewController:screen animated:YES];
}
Im hoping to get some clarification on what im not doing right!
thanks,
David
Two things you could change before trying again:
after both while loops, check that responder is not nil before doing dismiss or present a viewcontroller
can't you just assign screen.peoplePickerDelegate = self; instead of using this scr?

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.

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.