iOS application crashes when button is clicked iPhone - iphone

I am using xcode 4.2 and ARC. Below is the code I am using. When I click on any button my application crashes and it highlights this code in main.m
Sometimes I get this error
-[__NSCFTimer parentLogin:]: unrecognized selector sent to instance
and sometimes application crashes without any error.
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
My code is:
In my ViewController.m I am using this code to get to the ChooseLogin view controller.
- (IBAction)action:(id)sender {
ChooseLogin *loginScreen = [[ChooseLogin alloc] initWithNibName:#"ChooseLogin" bundle:nil];
[UIView beginAnimations:#"flipview" context:nil];
[UIView setAnimationDuration:2];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.view cache:NO];
[self.view addSubview:loginScreen.view];
[UIView commitAnimations];
}
Then in ChooseLogin.m:
#implementation ChooseLogin
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (IBAction)parentLogin:(id)sender {
NSLog(#":::::::");
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *passwd = [prefs stringForKey:#"password"];
NSLog(#"data saved");
if (passwd == nil) {
CreatePassword *cPassword = [[CreatePassword alloc] initWithNibName:#"CreatePassword" bundle:nil ];
[UIView beginAnimations:#"flipview" context:nil];
[UIView setAnimationDuration:1];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.view cache:NO];
[self.view addSubview:cPassword.view];
[UIView commitAnimations];
}else {
UserPassword *uPasswd = [[UserPassword alloc] initWithNibName:#"UserPassword" bundle:nil];
[UIView beginAnimations:#"flipview" context:nil];
[UIView setAnimationDuration:1];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.view cache:NO];
[self.view addSubview:uPasswd.view];
[UIView commitAnimations];
}
}
- (IBAction)childLogin:(id)sender {
ChildLogin *loginScreen = [[ChildLogin alloc] initWithNibName:#"ChildLogin" bundle:nil];
[UIView beginAnimations:#"flipview" context:nil];
// [UIView setAnimationDuration:1];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.view cache:NO];
[self.view addSubview:loginScreen.view];
[UIView commitAnimations];
}
#end

-[__NSCFTimer parentLogin:]: unrecognized selector sent to instance
This means that a message is being sent to a class that the class doesn't recognize -- you either haven't written this custom selector (function), or somehow a function is being sent to the wrong class and therefore isn't recognized. XCode is usually pretty good about catching these when trying to write code, but it doesn't seem to check for them in XIB files.
The most frequent cause of this type of issue, in my experience, is when you delete or rename a function, and forget to update the XIB file associated with that class -- or you do update it, and it adds the new version of the function, without forgetting the old one.

You should not add view of one view controller to another.
If you still want to do this. You create controller, take it's view amnd then it get released, because you store reference to it nowhere. make ivar loginScreen and ARC will retain ChooseLogin controller for you.

Related

How to retain superview when adding a subview to it in iphone?

I had an application in which i am having a view controller which is an appdelegate instance.I am adding that view to the self.navigationcontrollers view in order to place it above navigation bar.lke this `
if(appDelegate.viewcontroller==nil)
{
appDelegate.viewcontroller = [[ViewController alloc] init];
[self.navigationController.view addSubview:appDelegate.viewcontroller.view];
NSLog(#"My view frame: %#", NSStringFromCGRect(appDelegate.viewcontroller.view.frame));
appDelegate.viewcontroller.view.tag=7;
appDelegate.viewcontroller.view.frame =CGRectMake(0,480,300,460);
[UIView animateWithDuration:.50
animations:^{
appDelegate.viewcontroller.view.frame =CGRectMake(0,30,300,440);
}];
[appDelegate.viewcontroller viewWillAppear:YES];
}
else
{
NSLog(#"My view frame: %#", NSStringFromCGRect(appDelegate.viewcontroller.view.frame));
appDelegate.viewcontroller.view.frame=CGRectMake(0, 30,300, 440);
[self.navigationController.view addSubview:appDelegate.viewcontroller.view];
appDelegate.viewcontroller.view.frame =CGRectMake(0,480,300,460);
[UIView animateWithDuration:.50
animations:^{
appDelegate.viewcontroller.view.frame =CGRectMake(0,30,300,440);
}];
[appDelegate.viewcontroller viewWillAppear:YES];
}
...Now i am adding another viewcontrollers view in to this view by
contactphoneForAddfriend= [[EventsViewController alloc]initWithNibName:#"EventsViewController" bundle:nil];
contactphoneForAddfriend.orgarray=self.eventsArray;
[UIView beginAnimations:#"View Flip" context:nil];
[UIView setAnimationDuration:0.80];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationTransition:
UIViewAnimationTransitionFlipFromRight
forView:self.view cache:NO];
[self.view addSubview:contactphoneForAddfriend.view];
[UIView commitAnimations];
` and then from that view controller when i am going in to the background of the application ...that appdelegate viewcontroller is showing as nil.How come this happen.after all its an appdelegate variouble.when i remove he added view and then go background it is there.Can anybody point me in where i am going wrong?
Checked you code.And found viewController is not nil.Check yourself by applying some code in this method
- (void)applicationDidEnterBackground:(UIApplication *)application
{
if (self.viewController==nil) {
NSLog(#"ViewController is nil");
}else{
NSLog(#"ViewController Has value %#",NSStringFromClass([self.viewController class]));
}
}

ViewController transition

I have code:
ListViewController * listViewController = [[ListViewController alloc] initWithNibName:#"ListViewController" bundle:nil];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES];
[self viewWillDisappear:YES];
[listViewController viewWillAppear:YES];
self.view.hidden = YES;
listViewController.view.hidden = NO;
[self viewDidDisappear:YES];
[listViewController viewDidAppear:YES];
[UIView commitAnimations];
But it does not works, and listViewController does not displayed( Please, somebody can tell me the solution of this problem?
Try something like:
UIViewAnimationOptions ops = UIViewAnimationOptionTransitionFlipFromRight;
NSArray *temp = [[NSBundle mainBundle] loadNibNamed:#"NameOfNib" owner:self options:nil];
UIView* newView = [[temp objectAtIndex:0] view];
[UIView transitionFromView:self.view toView:newView duration:1.5 options:ops completion:nil];
self.view = newView; //Lets you control the new view from the current controller (you might want to save a reference to the old one if you need to change back)
As meronix said, non-block based animation is discouraged by Apple for the newer iOS versions. The above method is the "approved" way to do it.
Just so you know, the viewWillAppear, viewDidDisappear, and similar methods aren't methods that YOU call to make the view do things. They're called automatically when these things happen.
Your code had a few misunderstandings; I've commented on them below
//This looks fine (depending on what is in the nib)
ListViewController * listViewController = [[ListViewController alloc] initWithNibName:#"ListViewController" bundle:nil];
//Normally I use these to move things around, not change the view
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES];
[self viewWillDisappear:YES]; //These two methods aren't things that you call
[listViewController viewWillAppear:YES];
self.view.hidden = YES; //If you're flipping or otherwise moving a view out of
listViewController.view.hidden = NO; //sight then you don't need to hide/unhide views
[self viewDidDisappear:YES]; //Same as above, you don't call these
[listViewController viewDidAppear:YES];
[UIView commitAnimations];
Remove unnecessary code and just write this ...
ListViewController * listViewController = [[ListViewController alloc] initWithNibName:#"ListViewController" bundle:nil];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES];
[self.view addSubview:listViewController.view];
[UIView commitAnimations];
it cannot work!
you just create and allocate a UIViewController, but never push it on any stacks, or add its view to a visible view.
when you set listViewController.view.hidden to no, you are not magically showing it on screen: you need to add its view to a view (or window) which is already on screen...
ps beginAnimation is deprecated: use blocks animation instead...

Why do I see jerky animation when using this UIView animation block?

I have a a tab bar that is made in the application delegate. By calling an action form a button click from one of the views loaded from the tab bar, I open the help screen but there is a jerking motion after loading.
forgive me for speaking informally..I have been picking my brain for the past few hours trying to figure this out..
-(void)flipToHelp {
HelpViewController *helpVariable = [[HelpViewController alloc] initWithNibName:#"HelpView" bundle:nil];
[self setHelpViewController:helpVariable];
[UIView beginAnimations:#"flipview" context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft
forView:_window cache:YES];
[_window removeFromSuperview];
[helpVariable release];
self.window.rootViewController = self.HelpViewController;
[UIView commitAnimations];
}
Just to reiterate from the comment thread, you shouldn't be removing the window from its superview (it doesn't technically have a superview, so it's probably causing problems). And setting the window's rootViewController property should swap out the view hierarchies, Apparently the jerkiness comes from changing the window's rootViewController property, so maybe the solution is to avoid using that property. Here's what I think should be enough to accomplish this:
-(void)flipToHelp {
HelpViewController *helpVariable = [[HelpViewController alloc] initWithNibName:#"HelpView" bundle:nil];
[UIView beginAnimations:#"flipview" context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:_window cache:YES];
[self.tabBarController removeFromSuperview];
[_window addSubview:helpVariable];
[UIView commitAnimations];
}
-(void)flipToHelp {
HelpViewController *helpVariable = [[HelpViewController alloc] initWithNibName:#"HelpView" bundle:nil];
[self setHelpViewController:helpVariable];
[helpVariable release];
[UIView beginAnimations:#"flipview" context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft
forView:self.window
cache:YES];
self.window.rootViewController = self.HelpViewController;
[UIView commitAnimations];
}
How about this code? Does it still have a jerky animation?

Passing arguments in a selector in IPhone

I have a barButtonItem that calls an action which swaps two views in an animation block. I disable the barButtonItem at the beginning of the action method that it calls, and the I want to enable it when its finished, so that the user can't flip the view again before it has finished flipping.
It does this in a UIView animation block, so If I enable it at the ned of the method, it will be instant and that destroys the purpose because it gets enabled before the animation finishes.
In the method, I cast sender to a UIBarButtonItem, and I disable that. So I found [UIView setAnimationDidStopSelector:#selector()], but I need to pass my barButtonItem in the selector so the enableControl Method will enable that (this would be a lot easier if I could make an anonymous function..).
So how can I pass the barButton as an argument? I'd rather not have to make an ivar just for this.. Here's the code:
- (void)barBtnItemSwitchViews_Clicked:(id)sender {
UIBarButtonItem *barButton = (UIBarButtonItem *)sender;
barButton.enabled = NO;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.8];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
if(!self.yellowVC.view.superview) {
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:innerView cache:YES];
[yellowVC viewWillAppear:YES];
[blueVC viewWillDisappear:YES];
[blueVC.view removeFromSuperview];
self.blueVC = nil;
[innerView addSubview:yellowVC.view];
} else if(!self.blueVC.view.superview) {
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:innerView cache:YES];
[blueVC viewWillAppear:YES];
[yellowVC viewWillDisappear:YES];
[yellowVC.view removeFromSuperview];
self.yellowVC = nil;
[innerView addSubview:blueVC.view];
}
[UIView commitAnimations];
[UIView setAnimationDidStopSelector:#selector(enableControl:)]; // How do I pass barButton??
}
- (void)enableControl:(UIControl *)control {
control.enabled = YES;
}
Here's what I changed it to, but the method isn't being called.. What am I doing wrong?
- (void)barBtnItemSwitchViews_Clicked:(id)sender {
UIBarButtonItem *barButton = (UIBarButtonItem *)sender;
barButton.enabled = NO;
[UIView beginAnimations:nil context:barButton];
[UIView setAnimationDuration:0.8];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
if(!self.yellowVC.view.superview) {
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:innerView cache:YES];
[yellowVC viewWillAppear:YES];
[blueVC viewWillDisappear:YES];
[blueVC.view removeFromSuperview];
self.blueVC = nil;
[innerView addSubview:yellowVC.view];
} else if(!self.blueVC.view.superview) {
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:innerView cache:YES];
[blueVC viewWillAppear:YES];
[yellowVC viewWillDisappear:YES];
[yellowVC.view removeFromSuperview];
self.yellowVC = nil;
[innerView addSubview:blueVC.view];
}
[UIView setAnimationDidStopSelector:#selector(flipViewAnimationDidStop:finished:context:)];
[UIView commitAnimations];
}
- (void)flipViewAnimationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
UIBarButtonItem *barButton = (UIBarButtonItem *)context;
barButton.enabled = NO;
NSLog(#"Disabled");
}
EDIT: I figured it out. I had to make the button the context, and then do setAnimationDelegate:self, and then setAnimationDidEndSelector:#selector(myMethod:finished:context:), and then in the method cast context to uibarbuttonitem and re-enable it.
From UIView's setAnimationDidStopSelector: docs:
The selector should be of the form: - (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context.
The third argument, context, is an object of your choice, set in beginAnimations:context:. So it seems like what you're missing is to use barButton as the context argument, and then rewrite enableControl with a proper signature. In that method, you take the third argument, context, and cast it from void* to a UIBarButtonItem*.
I dont think you can pass more than one argument to the selector by using #selector, however you could make your button a class variable and enable or disable it like that in the selector (instead of it being passed as an argument), to define a selector with multiple arguments you can use NSInvocation, like so
NSInvocation *inv = [[NSInvocation alloc] init];
[inv setSelector:#selector(foo:bar:)];
[inv setArgument:123 atIndex:0];
[inv setArgument:456 atIndex:1];
But I dont think this will work for what you are trying to do.

Problem Flipping a View

I'm having some trouble getting a view to flip. I have the following code in my View Controller:
- (void)loadFlipsideViewController {
ProblemViewFlipController *viewController = [[ProblemViewFlipController alloc] initWithNibName:#"ProblemViewFlip" bundle:nil];
self.problemViewFlipController = viewController;
[viewController release];
}
- (void) flipView {
if (problemViewFlipController == nil) {
[self loadFlipsideViewController];
}
UIView *mainView = self.view;
UIView *flipView = problemViewFlipController.view;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1];
[UIView setAnimationTransition:([mainView superview] ? UIViewAnimationTransitionFlipFromLeft : UIViewAnimationTransitionFlipFromRight) forView:self.view cache:YES];
if ([flipView superview])
{
[flipView removeFromSuperview];
[self.view addSubview:mainView];
}
else
{
[mainView removeFromSuperview];
[self.view addSubview:flipView];
}
[UIView commitAnimations];
}
The problem is, is that when I call flipView, the view is replaced with a blank view (i.e. nothing in the view I'm flipping to is displayed).
Is there something obvious I'm missing here? (I suspect there is!)
Not positive, but I think you need to use a 'controller' to flip the views. Looks like you're using one of the flipped views as the controller. Just add a root controller to flip your views.
Code like this should work from the root controller:
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES]; // self.view is the root controller's view
[mainViewController viewWillAppear:YES];
[flipViewController viewWillDisappear:YES];
[flipViewController.view removeFromSuperview];
[self.view addSubview:mainViewController.view];
[flipViewController viewDidDisappear:YES];
[mainViewController viewDidAppear:YES];
[UIView commitAnimations];
Based on the variable names, it looks like this is adapted from the default "Utility" application template you get from XCode. If the template project works, and yours doesn't, then you've obviously changed something you shouldn't have :-)
Chances are, one of the outlets in your controller that should be pointing at a view, isn't. Double-check your Nibs, and check the values of the outlets in the debugger. If all else fails, start over again from the template, and see at what point it stops working.