auto scroll uipickerview when button is clicked - objective-c

I am using a UIPickerView to display answers to a question. There needs to then be a button to reveal the correct answer if the user cannot work it out. I have tried the below, but the app crashes. How can i autoscroll to the correct answer?
-(void)reveal {
[myPickerView selectRow:0 inComponent:0 animated:YES];
}
EDIT: Yes i am just trying to get it to scroll to the first answer in the above example :)
[ExcerciseController reveal:]: unrecognized selector sent to instance 0x68398f0
2012-10-30 14:57:21.673 English[20786:f803] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[ExcerciseController reveal:]: unrecognized selector sent to instance 0x68398f0'
* First throw call stack:
(0x1397022 0x108ccd6 0x1398cbd 0x12fded0 0x12fdcb2 0x1398e99 0x1f214e 0x1f20e6 0x298ade 0x298fa7 0x298266 0x2173c0 0x2175e6 0x1fddc4 0x1f1634 0x21a3ef5 0x136b195 0x12cfff2 0x12ce8da 0x12cdd84 0x12cdc9b 0x21a27d8 0x21a288a 0x1ef626 0x2c5d 0x2b85)
terminate called throwing an exception[Switching to process 20786 thread 0xf803]

Now, that the error/crash message is visible, it turns out that the error is not in how you call the picker's method. It is how you invoke reveal.
You call reveal with some object. Or you call it by passing a selector. However, the current selector that you are calling is reveal: but the methods name is reveal without any parameter. reveal: would require one parameter.
Wherever you call it, make sure that you call reveal and not reveal:
I assume you set the selector either in interface builder or when creating the button programmatically. That is the place where you made the mistake.
Some background:
The error message tells you what you need to know. A method name is called selector in Objective-C at runtime. In some literature you will find the wording of a message (named reveal: in your case) is sent to a receiver named ExcerciseController in your case.
Objective-C is polymorph. That means that reveal (called without any parameters) refers to another method implementation than reveal: (called with one parameter) or even reveal:: (called with three parameters).

myPickerView may not be the instance of UIPickerView as UIPickerView responds to selector - (void)selectRow:(NSInteger)row inComponent:(NSInteger)component animated:(BOOL)animated
Or you have to check how the method reveal is invoked.

please try to post the crash description,Need more blocks of code to answer it :)
However just try the below code
myPickerView.delegate = self;
[myPickerView reloadAllComponents];
[myPickerView selectRow:0 inComponent:0 animated:YES];

Related

Difficulty implementing delegate methods in an objective-c project

I have a table view which I am using as a form.
The first cell has an editable text field. The second cell, when clicked, loads a semi modal date picker. I used this: http://reednj.tumblr.com/post/1346445326/ios-semi-modal-date-picker-for-iphone
Anyway, I've defined my datepicker and set its delegate inside the tableviewcontroller like so:
TDDatePickerController* datePickerView = [[TDDatePickerController alloc]
initWithNibName:#"TDDatePickerController"
bundle:nil];
[datePickerView setDelegate:self];
[self presentSemiModalViewController:datePickerView];
Now the methods to handle the button clicks are defined as
-(void)datePickerSetDate:(TDDatePickerController*)viewController;
-(void)datePickerClearDate:(TDDatePickerController*)viewController;
-(void)datePickerCancel:(TDDatePickerController*)viewController;
So when the cancel button is pressed I would like the semi-modal date picker to be dismissed, So I've got:
-(void)datePickerCancel:(TDDatePickerController*)viewController{
[self dismissSemiModalViewController:viewController];
}
But this makes the app crash with the error
-[__NSCFType cancelDateEdit:]: unrecognized selector sent to instance 0xe738c00
2012-09-18 10:23:47.254 JRPBaseCamp[7612:f803] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFType cancelDateEdit:]: unrecognized selector sent to instance 0xe738c00'
So I looked up the cancelDateEdit method in TDDatePickerController:
-(IBAction)cancelDateEdit:(id)sender {
if([self.delegate respondsToSelector:#selector(datePickerCancel:)]) {
[self.delegate datePickerCancel:self];
}
}
What am I doing wrong? It must be something obvious that I'm overlooking. Any help is much appreciated, I'm fairly new to iOS and the whole concept of delegates is really confusing me.
Thanks!
The unrecognized selector sent means nothing is responding to cancelDateEdit: selector. You can use the debugger to search what kind of object is 0xe738c00 and then look for the appropriate selector there.

[__NSCFType searchKeyword:]: unrecognized selector sent to instance 0x6d8eb80

The following code is to add a subview to current view from storyboard:
EventSearchViewController* view1 = [self.storyboard instantiateViewControllerWithIdentifier:#"searchView"];
[view1 setBookingSystem:system];
[self.view addSubview:view1.view];
In the view "view1", there is a textField. The following is a IBAction to the textField and the event is "Did end on exit".
-(IBAction)searchKeyword:(id *)sender
{
NSLog(#"searchKeyword");
}
The following is the error message.
2012-05-26 20:26:47.369 OnlineBooking[6607:f803] -[__NSCFType searchKeyword:]: unrecognized selector sent to instance 0x6d8eb80
2012-05-26 20:26:47.369 OnlineBooking[6607:f803] * WebKit discarded an uncaught exception in the webView:shouldInsertText:replacingDOMRange:givenAction: delegate: -[__NSCFType searchKeyword:]: unrecognized selector sent to instance 0x6d8eb80
You need to retain your EventSearchViewController, or keep a strong reference to it if you're using ARC. If you assign it to view1 as a local variable, it's not going to be around any more when searchKeyword: gets called. (The error shows that its memory has been released and re-used for a different type of object.)
For me the problem was that I never called
- (void)removeTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents
before I called
- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents
again.

Crash from attempting to clear a calcuator

Hey I'm trying to make a clear button for my polish calculator.. code give me this error when executing the clear button
unrecognized selector sent to instance 0x6a6e1e0'
there is a button in the interface builder linked to clearBtn
this code is in the CalculatorViewController.m
display is linked to the UILabel
and..
heres the code
- (IBAction)clearBtn
{
if (self.userIsInTheMiddleOfEnteringANumber) {
[[self display] setText:#"0"];
[self setUserIsInTheMiddleOfEnteringANumber:NO];
}
}
- (IBAction)clearAllBtn //this button works fine..
{
[[self brain] clearAll]; // the brain class has a method to set the array to nill
[[self display] setText:#"0"];
[self setUserIsInTheMiddleOfEnteringANumber:NO];
}
kind of hard to know without seeing the rest of your code, but I'd assuming it's because you don't have a method named setUserIsInTheMiddleOfEnteringANumber. Maybe double check the spelling and parameter list?
Thanks for posting the code. Generally, unrecognized selector errors are not tough, but we need to know which line of code is triggering it, which will help us understand which object is receiving the message, and what the selector is. Can you post a dump of the error messages?
Read about Objective-C selectors here.
Generally speaking, Interface Builder is a great tool for... umm, building interfaces. But you do have to be careful about connections and making sure everything is still linked up after making changes to your code or layout.

Objective-C Declaring object as a totally different class than what I declared

In a method I wrote, I'm declaring an instance of class 'A' and calling a method on it. At run time, when the method runs, my app crashes. It says an unrecognized selector was sent to an instance of class 'B' even though I declared an instance of 'A'. I read somewhere that I may not be managing my memory correctly so it's sending the method to another class, but I'm using ARC so that shouldn't even be a problem. Help would be much appreciated!
The error I'm getting:
2011-08-27 01:25:49.859 Intelligenda[49385:bc03] PVC: <HomeViewController: 0x59359e0>
2011-08-27 01:25:49.945 Intelligenda[49385:bc03] -[HomeViewController addNewClass:]: unrecognized selector sent to instance 0x59359e0
2011-08-27 01:25:49.947 Intelligenda[49385:bc03] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[HomeViewController addNewClass:]: unrecognized selector sent to instance 0x59359e0'
and the method that's being called:
-(IBAction)done:(id)sender{
[teacherName resignFirstResponder];
[className resignFirstResponder];
IntelligendaAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
UINavigationController *nav = delegate.navController;
[nav popViewControllerAnimated:YES];
ClassesViewController *classesParentViewController = (ClassesViewController *) nav.topViewController;
ClassIG *theNewClass = [[ClassIG alloc] init];
theNewClass.className = className.text;
theNewClass.teacherName = teacherName.text;
NSIndexPath *indexPath;
theNewClass.subject = [tableView cellForRowAtIndexPath:indexPath].textLabel.text;
// add reminder to array
NSLog(#"PVC: %#", [classesParentViewController description]);
[classesParentViewController addNewClass:theNewClass];
}
Declaring, say, a pointer NSArray* fred does nothing (other than a sprinkling of compile-time warning messages) to assure that fred is an NSArray. It's what you assign to fred that counts. If you assign an NSDateFormatter, then that's what fred is.
Your assumption that the application delegate's navigation controller has a ClassesViewController on top is clearly flawed. It has a HomeViewController on top. Your structure looks like it may be a little complicated, given that you have a button somewhere wired up to an action in some other part of your controller classes, so there could be a bunch of causative reasons.
First things to check: if you're instantiating buttons programmatically then did you accidentally add the button to the wrong controller? If you're designing everything using the graphical interface designer but keeping it in a single NIB, is it possible you copied and pasted a button and forgot to change what it's wired to?
If you can end up with multiple navigation controllers, are you sure the one held by the application delegate is currently on screen?
the short answer is that typecasting and type conversions are an exercise for the programmer in objc.
read my answer here:
Passing NSNumber* to NSString* expected-parameter does not cause compiler warning / error
...for some more information as to how declaring/casting a variable of an object differs from other languages, and how you can detect those cases.

How to Assign a UITextField to a NSString (with UITouchEvent error?)

I'm simply trying to assign a UITextField.text to an NSString in my object.
The code errors out when I try to print the enteredLocation.text but I have no idea why. I thought it was a type mismatch, but both are NSString. Any idea?
- (IBAction) submitPushed: (UITextField*)enteredName: (UITextField*)enteredCell: (UITextField*)enteredLocation;
{
ExampleAppDataObject* theDataObject = [self theAppDataObject];
NSLog(#"LOC %#", enteredLocation.text);
theDataObject.location = enteredLocation.text;
NSLog(#"DATAOBJ %#", theDataObject.location);
}
I always get this error:
2011-08-20 10:28:13.602 ViewControllerDataSharing[3188:207] -[UITouchesEvent text]: unrecognized selector sent to instance 0x4e07780
2011-08-20 10:28:13.607 ViewControllerDataSharing[3188:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITouchesEvent text]: unrecognized selector sent to instance 0x4e07780'
But UITouchEvent? How does that relate? I ctrl-clicked the button to the method in my .m file. Is there a step missing? I tried checking if the text is "", but it won't even get that far.
I have the I'm a major newbie, so thanks in advance. I've read 100s of answers on this site, and love it!! So helpful!
THANKS!
Mark
When the action fires for your button, the method signature will look like one of these:
-(IBACtion)onPushed; // no parameters
-(IBAction)onPushed:(id)sender; // sender is the control that initiated the event
-(IBAction)onPushed:(id)sender event:(UITouchesEvent*)event; // event is the touch event that caused the event
You can't control what gets passed to your handler by changing the parameters. If you need to access the text field, make sure there is an IBOutlet reference to the UITextField object in your class (and hook it up in IB) and use that to access it.
I'm guessing that you have the IBAction hooked up to the wrong action in your nib - one that is a touch event. If you try:
NSLog(#"Param1: %#", enterdLocation);
You'll see that the method is being passed a UITouchesEvent object instead of a UITextField object.
Double-check your nib connections.