Button in a popover will display text in the main view controller - objective-c

My popover has a button which when clicked, will display a text in the main view controller.
Here's my code for the button:
- (IBAction)print:(UIButton *)sender {
self.displayText.text= #"Hello World";
}
By the way, I followed the tutorial here.
I tried to run the program and I'm getting this error when I click the "print hello world" button:
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[PopoverViewController
print:]: unrecognized selector sent to instance 0xe379240'
I thought that when it's a button in popover, it's a different case. And i've followed few solutions to this problem (posted also by people who encountered the same problem) already but i can't seem to solve it. Still having errors. Hope you could help me.
Thank you.

When you set the action for the message, you need to send the message to your View Controller instead of to your Popover.
Which you can't really do when you have them in separate xib's.
There are a few different ways of approaching this, one of which would be to implement a delegate in your popover class, and when you create the popover set the delegate to self (the view controller which created it) and implement the delegate method. Then, when you press the button, it calls a function in your popover class which then calls your delegate's method.

Related

Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<AboutViewController 0x91cc1d0> setValue:forUndefinedKey:]: [duplicate]

This question already has answers here:
Xcode - How to fix 'NSUnknownKeyException', reason: … this class is not key value coding-compliant for the key X" error?
(79 answers)
Closed 7 years ago.
I am new to iPhone App development.
I got this error when I run my project
Terminating app due to uncaught exception 'NSUnknownKeyException',
reason: '[<AboutViewController 0x91cc1d0> setValue:forUndefinedKey:]:
It happens when I try to navigate to another view controller called AboutViewController.
I defined the rightBarButton like this
UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:#"About" style:UIBarButtonItemStylePlain target:self :#selector(showAbout)];
self.navigationItem.rightBarButtonItem = anotherButton;
The method showAbout is
- (void)showAbout{
AboutViewController *abvController = [[AboutViewController alloc] initWithNibName:#"AboutViewController" bundle:nil];
[self.navigationController pushViewController:abvController animated:YES];
[abvController release];
}
Also when I try to use presentModelViewController instead of navigationController, the compiler shows it is deprecated.
I found the problem, it was because of a button i used in the AboutViewController, i didnt declare the property for that button.
but can anyone tell me how to use ModelViewController to goto a new view in ios6, since when i use the method presentModelViewController, it shows a warning that it is deprecated
I think the problem is your AboutViewController, not in current view controller.
I guess your AboutViewController nib class is UIViewController class not AboutViewController class, try to change it to the right class. Click your nib file, and then click your file's owner, and then in the right section click third tab. You should see class in the top, change it to AboutViewCtroller.
Usually these types of error occur due to the problems with the outlets connected in xib.
Main scenarios:
View outlet is not connected in xib
Connected outlet's property is deleted from interface file
presentModelViewController is deprecated in iOS 6. Instead you can use presentViewController:animated:completion: for presenting the modalViews.
presentViewController:animated:completion:
Presents a view controller.
- (void)presentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completion
Parameters
viewControllerToPresent
The view controller being presented.
flag
Pass YES to animate the presentation; otherwise, pass NO.
completion
A completion handler or NULL.
Discussion
On iPhone and iPod touch, the presented view is always full screen. On
iPad, the presentation depends on the value in the
modalPresentationStyle property.
This method sets the presentedViewController property to the specified
view controller, resizes that view controller’s view and then adds the
view to the view hierarchy. The view is animated onscreen according to
the transition style specified in the modalTransitionStyle property of
the presented view controller.
The completion handler is called after the viewDidAppear: method is
called on the presented view controller.
Availability
Available in iOS 5.0 and later.
Declared In UIViewController.h
For more details check UIViewController Class

Button Click Gives Different Errors On Subsequent Launches

I've never seen this response before, where the same action, on different runs of the same app throws different errors.
I have an IBAction for a button (that just logs sender) in an NSViewController subclass that throws the errors upon clicking the button (this is an OSX app). I'm getting these (so far):
-[NSArrayM buttonClick:]: unrecognized selector sent to instance
-[__NSCFDictionary buttonClick:]: unrecognized selector sent to instance
-[__NSCFSet buttonClick:]: unrecognized selector sent to instance
-[NSRunLoop buttonClick:]: unrecognized selector sent to instance
And, the dreaded EXC_BAD_ACCESS.
The only code in this test app is this in the app delegate to instantiate the view controller:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
TestController *controller = [[TestController alloc] initWithNibName:#"TestController" bundle:nil];
[self.window.contentView addSubview:controller.view];
}
And this IBAction in the view controller subclass:
-(IBAction)buttonClick:(id)sender {
NSLog(#"%#",sender);
}
The IBAction which is implemented in the view controller is connected in IB (through File's Owner) to the button which is in its view.
I can't understand how I can get so many different error messages in such a simple program. Could this be a problem with the view controller not being in the responder chain? If so, why should it throw errors, shouldn't the message just get sent up the chain and then be discarded?
I've tried having the controller's view set the controller as the next responder (with [controller.view setNextResponder:controller];), but that just gives me the EXC_BAD_ACCESS error upon launch.
Ok, I fixed this with a pointer from Shane Stanley over at MacScripter (I had made another version in ApplescriptObjC to ask the question over there, but that version worked). It's a matter of memory management -- too easy to forget about that when using ARC. The TestController instance was being deallocated before I was sending the IBAction to it. This explains the variable error messages, because sending a message to a deallocated object can point to anything. The problem was fixed by declaring a property (as retain) for the TestController instance, controller.

How do you correctly instantiate and show a view programmatically in a Single View Application for the iPad

I would like to know how to correctly show a view programmatically. I will try to explain what i did so far:
The application has a storyboard with two view controllers. One of them has an identifier: " detailview". When i click on a button on the initial view controller i want to show the view controller with the identifier "detailview". Currently i'm using this code:
TI4ViewController* vc = [self.storyboard instantiateViewControllerWithIdentifier:#"detailview"];
UIWindow* window = [UIApplication sharedApplication].keyWindow;
[window addSubview:vc.view];
This works fine to show the view "detailview". But when i add a button on the detailview controller and add a touchdown event in the TI4ViewController(the view controller that's connected to "detailview") i'm getting an exception while clicking on this button.
I'm getting the following exception:
Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: '-[__NSCFType TestDown:]:
unrecognized selector sent to instance 0x6e0f670'
I'm using Xcode Version 4.3.2 (4E2002)
I'm not sure what i'm doing wrong here. I think the ViewController is released, but i'm not sure.
Instead of adding another view you can use segue. You can find an example here
Problem solved. I should have added the TI4ViewController as property of the initial view controller. Now it's removed from memory when you leave the function.

Multiview application crash

I made a multiview based application. My app has 3 views. The first is a disclaimer notice. When the user agrees it takes them to the main menu. From there, if they click a button, they will be taken to the respective views. One view is where the user can enter values (upon which a calculation is done). When I click the button to go to that view my app crashes and the following code gets highlighted. I followed this video tutorial.
[self presentModalViewController:second animated:YES]; along with the program received a SIGABRT message !
Checking the debugger showed me the following message: Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<gainview 0x6a10d10> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key label1.'
This is my full code:
code in the disclaimer view
-(IBAction)switchtoview2:(id)sender{
secondview *second = [[secondview alloc]initWithNibName:nil bundle:nil];
[self presentModalViewController:second animated:YES];
}
code in the main menu (i get the error when pressing the button in this view)
-(IBAction)swichtogain:(id)sender{
gainview *second = [[gainview alloc]initWithNibName:nil bundle:nil];
[self presentModalViewController:second animated:YES]; //debugger highlights this line !
}
When hitting the button it is supposed to go to another view where I have few buttons: UItextfields and few pickers.
Highly likely, you have a problem with one of your nib files, or a mismatch between nib definition and your classes.
In the nib associated to the controller that you are trying to show modally, check:
that all connections among outlets and objects are correct;
that the class identity of you nib objects is the one you expect to be (specifically for your controller/file's owner);
that your Objective C classes have all their IBOutlets defined.
If you need more help, you should post some more code...

IBAction UIButton and EXC_BAD_ACCESS

in .h file I write
-(IBAction)openShuffleForm;
and .m
-(IBAction)openShuffleForm{
NSLog(#"XXXXXXX");
}
and connect with even touch up inside
but when I run my program it error show this message
-[UITouchData openShuffleForm]: unrecognized selector sent to instance 0x391cc20
** What happen I don't know why?
Did you create the link between the button and the implementation? The implementing class should have an IBOutlet of type UIButton. Bind the IBOutlet to the button in Interface builder. This will cause the button to automatically be instantiated when the view is rendered and the BAD ACCESS should be alleviated.
In my case it was a modal window which I released after opening. Thus any button which I clicked to try to call ANY IBAction would not work from that Modal View and kill the app with SIGBART or BAD_EXEC errors.