setContentViewController method in popover iOS8 causes app crash - ios

The setContentViewController method in UIPopoverController seems to be causing an app crash in iOS 8. Just wondering if anybody else also faced this issue in iOS 8. This works without any issue in iOS 7.
The error pointed in the exception seems to be misleading as it states that the setContentViewController should be called after presenting the popover
- (void)buttonPressed {
UIViewController *tableViewController = [UIViewController new];
if(_popover == nil){
_popover = [[UIPopoverController alloc] initWithContentViewController:tableViewController];
[_popover presentPopoverFromRect:CGRectMake(self.textField.frame.size.width / 2, self.textField.frame.size.height / 1, 1, 1) inView:self.textField permittedArrowDirections:UIPopoverArrowDirectionLeft animated:YES];
}else{
[_popover setContentViewController:tableViewController];
}
}
Here is Stack trace from the crash,
2014-09-11 16:48:39.904 iOS 8 Rotation[3969:67869] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIPopoverController setContentViewController:animated:] can only be called after the popover has been presented.'
*** First throw call stack:
(
0 CoreFoundation 0x01c79df6 __exceptionPreprocess + 182
1 libobjc.A.dylib 0x01903a97 objc_exception_throw + 44
2 CoreFoundation 0x01c79d1d +[NSException raise:format:] + 141
3 UIKit 0x00b1946f -[UIPopoverPresentationController _setContentViewController:animated:] + 89
4 UIKit 0x009bb1b4 -[UIPopoverController setContentViewController:animated:] + 155
5 UIKit 0x009bb114 -[UIPopoverController setContentViewController:] + 48
6 iOS 8 Rotation 0x00046ca5 -[MianViewController buttonPressed] + 933
7 libobjc.A.dylib 0x019197cd -[NSObject performSelector:withObject:withObject:] + 84
8 UIKit 0x002ef79d -[UIApplication sendAction:to:from:forEvent:] + 99
9 UIKit 0x002ef72f -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 64
10 UIKit 0x00422a16 -[UIControl sendAction:to:forEvent:] + 69
11 UIKit 0x00422e33 -[UIControl _sendActionsForEvents:withEvent:] + 598
12 UIKit 0x0042209d -[UIControl touchesEnded:withEvent:] + 660
13 UIKit 0x0033faba -[UIWindow _sendTouchesForEvent:] + 874
14 UIKit 0x00340595 -[UIWindow sendEvent:] + 791
15 UIKit 0x00305aa9 -[UIApplication sendEvent:] + 242
16 UIKit 0x003158de _UIApplicationHandleEventFromQueueEvent + 20690
17 UIKit 0x002ea079 _UIApplicationHandleEventQueue + 2206
18 CoreFoundation 0x01b9d7bf __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
19 CoreFoundation 0x01b932cd __CFRunLoopDoSources0 + 253
20 CoreFoundation 0x01b92828 __CFRunLoopRun + 952
21 CoreFoundation 0x01b921ab CFRunLoopRunSpecific + 443
22 CoreFoundation 0x01b91fdb CFRunLoopRunInMode + 123
23 GraphicsServices 0x040cc24f GSEventRunModal + 192
24 GraphicsServices 0x040cc08c GSEventRun + 104
25 UIKit 0x002ede16 UIApplicationMain + 1526
26 iOS 8 Rotation 0x0004774d main + 141
27 libdyld.dylib 0x0224cac9 start + 1
28 ??? 0x00000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

I just encountered this same error testing our app under iOS 8. In our case, I took the error message at face value and changed a pattern we had in a few places.
The pattern we had to change was:
(1) in our view controller's init, instantiate a popover controller instance.
(2) on some event, set the popover controller's contentViewController property to the desired vc.
(3) call presentPopoverFromRect on the popover controller
and we simply changed step (2) to re-instantiate the popover controller with the desired content vc as an init parameter and ceased setting the contentViewController property (as we're always doing it prior to presenting the popover).

I was experiencing this same issue and finally solved it.
I have two buttons, each one shows its own popover. I was reusing the same UIPopoverController to show both of them. The first click worked fine, but then if you clicked the other one the app crashed.
The way I solved it is create a new UIPopoverController on each click:
importImagePickerControlPopoverController=[[UIPopoverController alloc] initWithContentViewController:pickerController];
[importImagePickerControlPopoverController setDelegate:self];
switch(pickerType)
{
case UIImagePickerControllerSourceTypePhotoLibrary:
case UIImagePickerControllerSourceTypeSavedPhotosAlbum:
[importImagePickerControlPopoverController setPopoverContentSize:CGSizeMake(320, 300) animated:YES];
break;
case UIImagePickerControllerSourceTypeCamera:
[importImagePickerControlPopoverController setPopoverContentSize:CGSizeMake(640, 640) animated:YES];
break;
}
[importImagePickerControlPopoverController setContentViewController:pickerController];

Similar to #user3495742 solution:
-(void)testAndSetPopoverWithVc:(UIViewController*)vc {
if (popover4Menu) {
if ([popover4Menu isPopoverVisible]) {
[popover4Menu setContentViewController:vc animated:YES];
return;
} else {
popover4Menu=nil;
}
};
popover4Menu=[[UIPopoverController alloc] initWithContentViewController:vc];
popover4Menu.delegate=self;
}
Problem rises when reassigning a view controller to an existing popover.
Free and realloc popover before assigning new content view controller: this fixed for me.

I had the same issue and this is what fixed it for me.
Try This
- (void)buttonPressed{
UIViewController *tableViewController = [UIViewController new];
//Check if the popover is nil or not visible
if(_popover == nil || _popover.popoverVisible == false){
_popover = [[UIPopoverController alloc] initWithContentViewController:tableViewController];
[_popover presentPopoverFromRect:CGRectMake(self.textField.frame.size.width / 2, self.textField.frame.size.height / 1, 1, 1) inView:self.textField permittedArrowDirections:UIPopoverArrowDirectionLeft animated:YES];
}else{
[_popover setContentViewController:tableViewController];
}
}

You have already have the content view controller set:
_popover = [[UIPopoverController alloc] initWithContentViewController:tableViewController];
↑
Here
So, why not just delete the line?:
[_popover setContentViewController:tableViewController];
That should work.

If popover is not visible when you are calling [_popover setContentViewController:tableViewController];, app will get crash.
Because this method should be called when popover is visible on the screen.
Make sure your popover is visible,
if(_popover != nil && [_popover isPopoverVisible] == YES)
{
[_popover setContentViewController:tableViewController];
}else
{
//create new popover object if _popover is nil or present it
}

[self.popover dismissPopoverAnimated:YES]; //in case it's already showing.
self.popover = nil; // N.B. this is not the same as popover = nil.
self.popover = [[UIPopoverController alloc] initWithContentViewController:tableViewController];
[self.popover presentPopoverFromRect:CGRectMake(self.textField.frame.size.width /
2, self.textField.frame.size.height / 1, 1, 1) inView:self.textField
permittedArrowDirections:UIPopoverArrowDirectionLeft animated:YES];

You do not need to create new instance of UIPopoverController neither set new contentViewController property after you first present UIPopoverViewController. (it depends on how you dismiss you popoverController)
However contentViewController can't be changed before popoverController presentation.
To workaround check popoverController.contentViewController properties. If it is nil, set conntentViewController, otherwise just present popover.
If you want to change contentViewController do it after presentation: use setContentViewController:animated: method.
Check that popoverController.isPopoverVisible before call this method.

Add these two methods into your UIwebview main class.
add <UIPopoverPresentationControllerDelegate> in your webview interface.h class as well.
define .
#property (strong, nonatomic) UIPopoverPresentationController *pop;
then
#synthesize pop;
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Assuming you've hooked this all up in a Storyboard with a popover presentation style
if ([segue.identifier isEqualToString:#"showPopover"]) {
UINavigationController *destNav = segue.destinationViewController;
pop = destNav.viewControllers.firstObject;
// This is the important part
UIPopoverPresentationController *popPC = destNav.popoverPresentationController;
popPC.delegate = self;
}
}
- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller {
return UIModalPresentationNone;
}

Related

Debugging UIKit crash [UINavigationController initWithRootViewController]

Xcode 7.0.1
Update:
The latest thing I tried was to break down creation of the UINavigationController thus:
self.viewController = [[ProjectsViewController alloc] initWithNibName:#"ProjectsViewController_iPhone" bundle:nil];
self.navigationController = [[UINavigationController alloc] init];
[self.navigationController setViewControllers:#[self.viewController]];
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
Doing this the crash is now on
[self.window makeKeyAndVisible];
but the trace is exactly the same.
I've also tried this with a vanilla ViewController by changing
[self.navigationController setViewControllers:#[[[UIViewController alloc] init]]];
Same result...
Original Post:
I have a crash which I am struggling to understand - Here is the lldb trace: Note the index of 2147483648
*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM removeObjectAtIndex:]: index 2147483648 beyond bounds [0 .. 2]'
*** First throw call stack:
(
0 CoreFoundation 0x035eaa94 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x03084e02 objc_exception_throw + 50
2 CoreFoundation 0x034f92ed -[__NSArrayM removeObjectAtIndex:] + 445
3 UIKit 0x018c20b2 -[UIView(Hierarchy) bringSubviewToFront:] + 260
4 UIKit 0x0193daeb -[UINavigationBar layoutSubviews] + 3692
5 UIKit 0x018d716b -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 813
6 libobjc.A.dylib 0x03099059 -[NSObject performSelector:withObject:] + 70
7 QuartzCore 0x0096e60c -[CALayer layoutSublayers] + 144
8 QuartzCore 0x0096228e _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 388
9 QuartzCore 0x00970b2c -[CALayer(CALayerPrivate) layoutBelowIfNeeded] + 44
10 UIKit 0x018c4dca -[UIView(Hierarchy) layoutBelowIfNeeded] + 1244
11 UIKit 0x01a117cf __74-[UINavigationController _positionNavigationBarHidden:edge:initialOffset:]_block_invoke + 36
12 UIKit 0x018caca6 +[UIView(Animation) performWithoutAnimation:] + 82
13 UIKit 0x01a1178d -[UINavigationController _positionNavigationBarHidden:edge:initialOffset:] + 922
14 UIKit 0x01a1194c -[UINavigationController _positionNavigationBarHidden:edge:] + 326
15 UIKit 0x01a12d5f -[UINavigationController _positionNavigationBarHidden:] + 49
16 UIKit 0x01a1104a -[UINavigationController setNavigationBar:] + 1224
17 UIKit 0x01a10a38 -[UINavigationController _navigationBarHiddenByDefault:] + 156
18 UIKit 0x01a10997 -[UINavigationController navigationBar] + 41
19 UIKit 0x01a17805 -[UINavigationController loadView] + 230
20 UIKit 0x019d3338 -[UIViewController loadViewIfRequired] + 138
21 UIKit 0x019d3cf1 -[UIViewController view] + 35
22 UIKit 0x01a22226 -[UINavigationController pushViewController:transition:forceImmediate:] + 615
23 UIKit 0x01a21e27 __54-[UINavigationController pushViewController:animated:]_block_invoke + 351
24 UIKit 0x01a21c83 -[UINavigationController pushViewController:animated:] + 786
25 UIKit 0x01a07be2 -[UINavigationController initWithRootViewController:] + 140
26 DELETIA 0x0012954e -[AppDelegate application:didFinishLaunchingWithOptions:] + 1214
This is a mature app which has been building and running for some time but in the current XCode the above happens.
As you can see there is a call to UINavigationController:initWithRootViewController - here is the code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// deletia - non UIKit code
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.tintColor = [UIColor darkGrayColor];
self.viewController = [[ProjectsViewController alloc] initWithNibName:#"ProjectsViewController_iPhone" bundle:nil];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
// deletia - but the app crashes on the above line
}
I have tried a few things after looking at some similar questions and
answers here on SO.
I've heard that this can happen if View controller-based status bar
appearance is set to YES in the Info.plist - so I've set that to NO /
YES
I've heard that some UIGestureRecognizers can cause issues - So I've examined the XIB and ensured that there are none effecting this view controller.
I've heard that if the root view controller isn't fully initialised
it can be problematic - so I've delayed the call to the
UINavigationController by 1 second
I've mis-trusted ProjectsViewController - so I've substituted it for
a vanilla UIViewController thus:
self.navigationController = [[UINavigationController alloc] initWithRootViewController:[[UIViewController alloc] init]];
Any insight much appreciated; either in what might be causing the issue or in a technique to debug that might throw some light on the issue.
I think that you are focusing on the wrong piece of code. As you mentioned, it crashes on the [self.window makeKeyAndVisible] line, however, what's probably causing the crash is the fact that this line causes the ProjectsViewController and the UINavigationController objects to load and be presented to the user. Looking at the crash log you posted, this is the part you should be investigating:
2 CoreFoundation 0x034f92ed -[__NSArrayM removeObjectAtIndex:] + 445
3 UIKit 0x018c20b2 -[UIView(Hierarchy) bringSubviewToFront:] + 260
4 UIKit 0x0193daeb -[UINavigationBar layoutSubviews] + 3692
You can see here that iOS attempts to layout the UINavigationBar's subviews and then to remove an object at an index that is probably NSNotFound (which would result in NSIntegerMax, which matches the index you mentioned in your crash log).
In order to further research the crash, I would recommend following these steps:
Replace your ProjectsViewController instance with a UIViewController instance without a custom .xib file. (I read in your original post that you have already tried this, but still, I would recommend doing this as the first step in order to eliminate additional issues, if there is more than one, on the way to fully resolving the crash). It's important to make sure that you are not using your .xib file at this point, as it may be what's causing the crash (if there are any mis-linked outlets or similar issues).
Does your navigation bar have any items displayed in it? Again, I noticed you mentioned in the comments that it doesn't, but I would double check to see if maybe there's a code section in which items are added to the bar under certain conditions and removed if they aren't met. This can cause the crash, if the items are attempted to be removed from an invalid index.
Try searching for places in your code that specifically call removeObjectAtIndex or any other NSMutableArray related call that may be called during the load and display process of your initial view controller. Add a breakpoint in these places to see if they are reached during the initial loading process. If so, try adding a test there to make sure that the index you are trying to remove an object from is larger than or equal to zero and is smaller than the array's size. That way, if index you are trying to access is NSNotFound it will at least not cause the app to crash (a good practice regardless of the current crash). If this ends up resolving the issue, you can then investigate the issue further and try to understand why the NSNotFound is actually received as the index and fix the issue logically.
I hope one of these steps can help identify and resolve your issue. Good Luck!
UINavigationController is a stack where you can only push and pop UIViewController. You should pass a UIViewController on initiating navigation controller. But if you don't know RootViewController then you can do like this.
self.viewController = [[ProjectsViewController alloc] initWithNibName:#"ProjectsViewController_iPhone" bundle:nil];
self.navigationController = [[UINavigationController alloc] init];
[self.navigationController pushViewController:self.viewController animated:NO];
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];

How to pass indexPath from segue to destination view?

I have two buttons in a custom layout collectionView's footer. I want to pass the indexPath of the footerView to destinationView class when user taps the button.
I wrote my code in a typical way which I succeeded before.
However, in some reason, whenever I tap the button it crashes.
I tried different approaches for each button to accomplish this job but neither worked. I have set destinationView's class to the right class in a storyboard. Please somebody help me. Any help with code example will be very appreciated since English is not my first language. Thank you.
Here's my code
// perform segue passing indexPath information
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// approach 1 : pass dictionary
if([[segue identifier] isEqualToString:#"SeeAllPictures"]) {
Footer *footerView = (Footer *)((UIButton *)sender).superview;
// debug code : working well
NSLog(#"perform segue from indexpath {%ld - %ld}", (long) footerView.indexPath.section, (long) footerView.indexPath.row);
SeeAllPicture *destVC = (SeeAllPicture *)[segue destinationViewController];
NSDictionary *indexPathDic = #{#"key1":footerView.indexPath};
// crashes at below line
destVC.indexPathDic = indexPathDic;
}
// approach 2 : pass indexPath.section, the NSInteger value
else if([[segue identifier] isEqualToString:#"GoToDiary"]) {
Footer *footerView = (Footer *)((UIButton *)sender).superview;
// debug code : working well
NSLog(#"perform segue from indexpath {%ld - %ld}", (long) footerView.indexPath.section, (long) footerView.indexPath.row);
Diary *destVC = (Diary *)[segue destinationViewController];
// crashes at below line
destVC.section = footerView.indexPath.section;
}
}
below is header of one of the destination class
#import <UIKit/UIKit.h>
#interface SeeAllPicture : UIViewController
#property (strong, nonatomic) IBOutlet UILabel *titleLabel;
#property (strong, nonatomic) NSDictionary *indexPathDic;
#property NSInteger section;
#end
And this is the message when my app crashes
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UINavigationController setIndexPathDic:]: unrecognized selector sent to instance 0x7ff8c8c4f140'
*** First throw call stack:
(
0 CoreFoundation 0x000000010f7f7a75 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x000000010f490bb7 objc_exception_throw + 45
2 CoreFoundation 0x000000010f7fed1d - [NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x000000010f7569dc ___forwarding___ + 988
4 CoreFoundation 0x000000010f756578 _CF_forwarding_prep_0 + 120
5 InfanTree 0x000000010ea1113b - [ViewController prepareForSegue:sender:] + 635
6 UIKit 0x000000011037e3cc - [UIStoryboardSegueTemplate _perform:] + 151
7 UIKit 0x000000010fe2ba22 - [UIApplication sendAction:to:from:forEvent:] + 75
8 UIKit 0x000000010ff32e50 -[UIControl _sendActionsForEvents:withEvent:] + 467
9 UIKit 0x000000010ff3221f -[UIControl touchesEnded:withEvent:] + 522
10 UIKit 0x00000001101d9e80 _UIGestureRecognizerUpdate + 9487
11 UIKit 0x000000010fe71856 -[UIWindow _sendGesturesForEvent:] + 1041
12 UIKit 0x000000010fe72483 -[UIWindow sendEvent:] + 667
13 UIKit 0x000000010fe3efb1 - [UIApplication sendEvent:] + 246
14 UIKit 0x000000010fe4c227 _UIApplicationHandleEventFromQueueEvent + 17700
15 UIKit 0x000000010fe2723c _UIApplicationHandleEventQueue + 2066
16 CoreFoundation 0x000000010f72cc91 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
17 CoreFoundation 0x000000010f722b5d __CFRunLoopDoSources0 + 269
18 CoreFoundation 0x000000010f722194 __CFRunLoopRun + 868
19 CoreFoundation 0x000000010f721bc6 CFRunLoopRunSpecific + 470
20 GraphicsServices 0x0000000112950a58 GSEventRunModal + 161
21 UIKit 0x000000010fe2a580 UIApplicationMain + 1282
22 InfanTree 0x000000010ea1b7c3 main + 115
23 libdyld.dylib 0x0000000113211145 start + 1
)
It looks like your SeeAllPicture view controller is embedded in a UINavigationController. In this case destinationViewController on the segue is that navigation controller NOT your SeeAllPicture view controller.
You probably want the topViewController of the destinationViewController
UINavigationController *navigationVC = (UINavigationController *)[segue destinationViewController];
SeeAllPicture *destVC = navigationVC.topViewController;
as rdelmar says its fine, but to have it crash free you could checko for the Class with isKindOfClass:. Anyways, just set a breakpoint after
SeeAllPicture *destVC = (SeeAllPicture *)[segue destinationViewController];
and check the console in XCode, here u will se that the class is kind of UINavigationController :)

App crashing in UIPopoverPresentationController but no explicit popovers?

My app (iOS 8 only) has been rejected due to a crash when IAP are attempted. I've tried pretty much every incantation of the purchase process in an AdHoc build but cannot reproduce a crash. Looking at the crash log that the review team attached, I am seeing a very weird stack trace in the last exception backtrace. The crash looks to be involving UIPopoverController, however my app, though universal, does not explicitly or implicitly display popovers anywhere. Does anyone have any idea what might trigger the activity that is causing this crash? What might cause my app to display popovers when the review team is looking at it only?
Last Exception Backtrace:
0 CoreFoundation 0x186d52084 __exceptionPreprocess + 132
1 libobjc.A.dylib 0x1977a40e4 objc_exception_throw + 60
2 UIKit 0x18bc0aee0 -[UIPopoverPresentationController presentationTransitionWillBegin] + 2464
3 UIKit 0x18b7d27d8 __71-[UIPresentationController _initViewHierarchyForPresentationSuperview:]_block_invoke + 1324
4 UIKit 0x18b7d1310 __56-[UIPresentationController runTransitionForCurrentState]_block_invoke + 212
5 UIKit 0x18b557388 _applyBlockToCFArrayCopiedToStack + 356
6 UIKit 0x18b4c8e4c _afterCACommitHandler + 532
7 CoreFoundation 0x186d0a388 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 32
8 CoreFoundation 0x186d07314 __CFRunLoopDoObservers + 360
9 CoreFoundation 0x186d076f4 __CFRunLoopRun + 836
10 CoreFoundation 0x186c35664 CFRunLoopRunSpecific + 396
11 GraphicsServices 0x18fd435a4 GSEventRunModal + 168
12 UIKit 0x18b53a984 UIApplicationMain + 1488
Not sure if it's the same cause as the original question, but I have the exact same error and the issue was using a UIAlertController with an ActionSheet style, presenting it worked fine on iPhone but iPad requires a sourceview to be set - https://stackoverflow.com/a/24233937/285694
This is a just similar situation. I had a crash bug on [UIPopoverPresentationController presentationTransitionWillBegin] on iOS 9+, and turns out that the crash occurred when sourceView was nil.
Example (in Objective-C):
UIViewController *vc = <#instance#>.
vc.modalPresentationStyle = UIModalPresentationPopover;
vc.popoverPresentationController.delegate = self;
vc.popoverPresentationController.sourceView = sourceView; // <--- this MUST NOT be nil.
vc.popoverPresentationController.sourceRect = sourceView.bounds;
[self presentViewController:vc animated:YES completion:nil];
It most probable the ActionSheet crash in iPad.
You should have a if condition like:
if let popoverController = alertVC.popoverPresentationController {
popoverController.sourceView = self.view
popoverController.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.midY, width: 0, height: 0)
popoverController.permittedArrowDirections = []
}
First you should check UIPopoverPresentationController available or not.
NSArray *Items = [NSArray arrayWithObjects:emailBody,anImage, nil];
UIActivityViewController *activityController = [[UIActivityViewController alloc] initWithActivityItems:Items applicationActivities:nil];
if ([UIPopoverPresentationController class] != nil) {
UIPopoverPresentationController *popover = activityController.popoverPresentationController;
if (popover)
{
popover.sourceView = sender;
//popover.sourceRect = sender.bounds;
popover.permittedArrowDirections = UIPopoverArrowDirectionAny;
}
}
[self presentViewController:activityController animated:YES completion:NULL];

PushViewController breaks my program when in if statement

I have already created a navigation controller in another class with a root view controller(the login screen). When I hit the login button I want it to push to the dashboard view controller. Here is my code
- (IBAction)logInHit:(id)sender {
if (passWord == true) {
DashBoardViewController *dash = [[DashBoardViewController alloc] initWithNibName:#"DashBoardViewController" bundle:nil];
[self.navigationController pushViewController:dash animated:YES];
}
else if (passWord == false){
UIAlertView *alert = [[UIAlertView alloc] init];
[alert setTitle:#"Incorrect Password"];
[alert setMessage:#""];
[alert setDelegate:self];
[alert addButtonWithTitle:#"Try Again"];
[alert show];
}
}
It works when it pops a view controller and it also works when it is not in the if statement but for some odd reason it breaks when using the code above. The alert view works fine! It is just the push! Please help!
In the App Delegate.h:
NavigationViewController *navView;
Here is the App Delegate (This creates an instance of the navigationController class that I made which is based off of the generic one):
LogInViewController *logInView = [[LogInViewController alloc] init];
navView = [[NavigationViewController alloc] initWithRootViewController:logInView];
[self.window addSubview:navView.view];
Here is the Log:
2014-03-06 22:15:43.552 TopOPPS REP APP[795:70b] Application windows are expected to have a root view controller at the end of application launch
2014-03-06 22:16:18.728 TopOPPS REP APP[795:70b] -[LogInViewController textEnded:]: unrecognized selector sent to instance 0x109412a90
2014-03-06 22:16:18.730 TopOPPS REP APP[795:70b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[LogInViewController textEnded:]: unrecognized selector sent to instance 0x109412a90'
*** First throw call stack:
(
0 CoreFoundation 0x0000000101890795 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x00000001015f3991 objc_exception_throw + 43
2 CoreFoundation 0x0000000101921bad -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x000000010188209d ___forwarding___ + 973
4 CoreFoundation 0x0000000101881c48 _CF_forwarding_prep_0 + 120
5 UIKit 0x00000001002570ae -[UIApplication sendAction:to:from:forEvent:] + 104
6 UIKit 0x0000000100257044 -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 17
7 UIKit 0x000000010032b450 -[UIControl _sendActionsForEvents:withEvent:] + 203
8 UIKit 0x000000010085a6d5 -[UITextField _resignFirstResponder] + 256
9 UIKit 0x000000010037ee40 -[UIResponder resignFirstResponder] + 222
10 UIKit 0x000000010085a4de -[UITextField resignFirstResponder] + 114
11 UIKit 0x000000010029b888 -[UIView setUserInteractionEnabled:] + 285
12 UIKit 0x000000010087f7b5 -[_UIViewControllerTransitionContext _disableInteractionForViews:] + 194
13 UIKit 0x0000000100364ce5 -[UINavigationController pushViewController:transition:forceImmediate:] + 1038
14 TopOPPS REP APP 0x000000010000254a -[LogInViewController logInHit:] + 202
15 UIKit 0x0000000100257096 -[UIApplication sendAction:to:from:forEvent:] + 80
16 UIKit 0x0000000100257044 -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 17
17 UIKit 0x000000010032b450 -[UIControl _sendActionsForEvents:withEvent:] + 203
18 UIKit 0x000000010032a9c0 -[UIControl touchesEnded:withEvent:] + 530
19 UIKit 0x000000010028bc15 -[UIWindow _sendTouchesForEvent:] + 701
20 UIKit 0x000000010028c633 -[UIWindow sendEvent:] + 988
21 UIKit 0x0000000100265fa2 -[UIApplication sendEvent:] + 211
22 UIKit 0x0000000100253d7f _UIApplicationHandleEventQueue + 9549
23 CoreFoundation 0x000000010181fec1 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
24 CoreFoundation 0x000000010181f792 __CFRunLoopDoSources0 + 242
25 CoreFoundation 0x000000010183b61f __CFRunLoopRun + 767
26 CoreFoundation 0x000000010183af33 CFRunLoopRunSpecific + 467
27 GraphicsServices 0x00000001039983a0 GSEventRunModal + 161
28 UIKit 0x0000000100256043 UIApplicationMain + 1010
29 TopOPPS REP APP 0x0000000100001d53 main + 115
30 libdyld.dylib 0x0000000101f1f5fd start + 1
31 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
Ok I got it!
In the NIB file of "LogInViewController", you created an Action called "textEnded", and after that you removed that method, but not the reference from the NIB.
To fix this, go to the LogInViewController NIB to see the Interface Builder, click on your UITextFields, and look inside the Connections Inspector for the method "textEnded" on the Sent Events category. After that, click on the "X" icon to remove the reference.
See this image as a reference:
1st You have to set root view Controller in appDelegate like,
LogInViewController *loginVC = [[LogInViewController alloc] initWithNibName:#"LogInViewController" bundle:nil];
UINavigationController *navController=[[UINavigationController alloc]initWithRootViewController:loginVC];
self.window.rootViewController = navController;
I think you are not familiar with if condition.
You should write like this
- (IBAction)logInHit:(id)sender {
if (passWord ) {
DashBoardViewController *dash = [[DashBoardViewController alloc] initWithNibName:#"DashBoardViewController" bundle:nil];
[self.navigationController pushViewController:dash animated:YES];
}
else {
UIAlertView *alert = [[UIAlertView alloc] init];
[alert setTitle:#"Incorrect Password"];
[alert setMessage:#""];
[alert setDelegate:self];
[alert addButtonWithTitle:#"Try Again"];
[alert show];
}
}
i checked your code its working complete fine...
but you have to set below in your appDelegate method
LogInViewController *loginVC = [[LogInViewController alloc] initWithNibName:#"LogInViewController" bundle:nil];
UINavigationController *navController=[[UINavigationController alloc]initWithRootViewController:loginVC];
self.window.rootViewController = navController;
as per your Console Log i can say that
you created one method called -(IBAction)textEnd:(id)sender{} before and linked that method to anywhere to button or textfied and now you removed that method and you forget to remove that method link from file inpector...
just double check your link from file inspector that all methods are available or not...
i got same error when i create -(IBAction)textEnd:(id)sender{} method and linked to UITextfield to EditingDidEnd action from file inpector and than i remove method whole method from my .m file and i got same error on click of login button so check your all links are have availabled linked methods

Terminating app due to uncaught exception 'NSInvalidArgumentException

I am writing iPad application and created DatePickerView and calling it from UIPopoverController as below:
DatePickerView *dt = [[DatePickerView alloc] initWithNibName:nil bundle:nil];
popover = [[UIPopoverController alloc] initWithContentViewController:dt];
popover.delegate = self;
[dt release];
self.popoverController = popover;
[popover release];
CGRect popoverRect = [self.view convertRect:[sender frame] fromView:[sender superview]];
popoverRect.size.width = MIN(popoverRect.size.width, 100);
[self.popoverController presentPopoverFromRect:popoverRect inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
In the DatePickerView i have below code when the "Done" button is selected, this is basically sending the selected date with formatter back to main UI by using appdelegate:
.h
I
BOutlet UIDatePicker *dtPicker;
-(IBAction) btnSelectClicked:(UIButton *)sender
{
MachineDetailsView *appDelegate;
appDelegate = (MachineDetailsView *)[[UIApplication sharedApplication] delegate];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"MM/dd/YYYY"];
NSDate *dt = [dtPicker date];
appDelegate.backPickerStartDate = [formatter stringFromDate:dt];
}
in MachineDetailsView i declared backPickerStartDate as below:
.h: file details
NSString *backPickerStartDate;
UIPopoverController *popoverController;
UIPopoverController *popover;
....
#property(nonatomic, retain) NSString *backPickerStartDate;
....
.m: file details
#synthesize backPickerStartDate;
I am getting below error when i click on "Done" button of DatePickerView
2011-03-23 11:46:59.813 iMobile[22492:40b] -[iMobileAppDelegate setBackPickerStartDate:]: unrecognized selector sent to instance 0x8a3dc00
2011-03-23 11:46:59.816 iMobile[22492:40b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[iMobileAppDelegate setBackPickerStartDate:]: unrecognized selector sent to instance 0x8a3dc00'
*** Call stack at first throw:
(
0 CoreFoundation 0x00df9be9 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x00f4e5c2 objc_exception_throw + 47
2 CoreFoundation 0x00dfb6fb -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
3 CoreFoundation 0x00d6b366 ___forwarding___ + 966
4 CoreFoundation 0x00d6af22 _CF_forwarding_prep_0 + 50
5 UIKit 0x00302a6e -[UIApplication sendAction:to:from:forEvent:] + 119
6 UIKit 0x003911b5 -[UIControl sendAction:to:forEvent:] + 67
7 UIKit 0x00393647 -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 527
8 UIKit 0x003921f4 -[UIControl touchesEnded:withEvent:] + 458
9 UIKit 0x003270d1 -[UIWindow _sendTouchesForEvent:] + 567
10 UIKit 0x0030837a -[UIApplication sendEvent:] + 447
11 UIKit 0x0030d732 _UIApplicationHandleEvent + 7576
12 GraphicsServices 0x0172fa36 PurpleEventCallback + 1550
13 CoreFoundation 0x00ddb064 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52
14 CoreFoundation 0x00d3b6f7 __CFRunLoopDoSource1 + 215
15 CoreFoundation 0x00d38983 __CFRunLoopRun + 979
16 CoreFoundation 0x00d38240 CFRunLoopRunSpecific + 208
17 CoreFoundation 0x00d38161 CFRunLoopRunInMode + 97
18 GraphicsServices 0x0172e268 GSEventRunModal + 217
19 GraphicsServices 0x0172e32d GSEventRun + 115
20 UIKit 0x0031142e UIApplicationMain + 1160
21 iG2Mobile 0x000022f2 main + 84
22 iG2Mobile 0x00002295 start + 53
)
terminate called after throwing an instance of 'NSException'
Program received signal: “SIGABRT”.
ANY HELP IS GREATLY APPRECIATED. Please let me know if any further details are needed.
I also added below setter method to MachineDetailsView but still no use:
-(void)setBackPickerStartDate:(NSString *)newBackPickerStartDate
{
if (backPickerStartDate != newBackPickerStartDate) {
backPickerStartDate = newBackPickerStartDate;
}
}
Thank you very much
You've stated that you've declared and synthesize the backPickerStartDate property in the MachineDetailsView class, yet you call it on an instance of iMobileAppDelegate in btnSelectClicked:
appDelegate.backPickerStartDate = [formatter stringFromDate:dt];
A few lines above, you have this:
MachineDetailsView *appDelegate;
appDelegate = (MachineDetailsView *)[[UIApplication sharedApplication] delegate];
That's wrong because you cast the pointer to your app delegate to a pointer to MachineDetailsView. That doesn't make your app delegate object magically turn into a MachineDetailsView object. What you need instead is a getter method or property that returns your MachineDetailsView object (or find another way to access it).
[iMobileAppDelegate setBackPickerStartDate:]: unrecognized selector sent to instance
As per the Exception above, you need to declare backPickerStartDate in your app delegate.

Resources