Unrecognized selector sent to instance weirdness - ios

This is extremely odd! Its simple stuff which is why i cannot understand. Below shows my class.
#import <UIKit/UIKit.h>
#interface LogInViewController : UIViewController
- (IBAction)loginButtonPressed:(id)sender;
#end
.
#import "LogInViewController.h"
#interface LogInViewController ()
#end
#implementation LogInViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//
- (IBAction)loginButtonPressed:(id)sender
{
NSLog(#"login Button Pressed");
}
Whenever i click the button though it throws this?!
loginButtonPressed:]: unrecognized selector sent to instance
0x15e3bf60 2013-10-28 11:23:24.249 Headache Mbl[1352:60b] *
Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: '-[__NSSetM
loginButtonPressed:]: unrecognized selector sent to instance
0x15e3bf60'
* First throw call stack:
Ive checked and the button is correctly sending the "touch up inside to loginButtonPressed:"
Help!, Thankyou:)
Here is how i attach the view controller to my main screen
import
#interface ViewController : UIViewController
#property (strong) IBOutlet UIView *loginView;
.
- (void)viewDidLoad
{
LogInViewController *logIn = [[LogInViewController alloc] initWithNibName:#"LogInViewController" bundle:nil];
[self.loginView addSubview:logIn.view];
[self.loginView setClipsToBounds:YES];
}

Looks like memory issues. If you are using ARC you should keep strong reference to view controller. If you are using manual memory management probably view controller has been overreleased. To find issues you can add NSZombieEnabled flag to scheme or use Instruments with Zombie.

Just a proposal, but did you check the event connections of the button in IB. May you left connection to another VC in which you've deleted the action method. Check in the console
loginButtonPressed:]: unrecognized selector sent to instance 0x15e3bf60
what is 0x15e3bf60 (po 0x15e3bf60).

Related

UITextField Not really working

I'm just starting to learn iOS development and Objective C and I'm following Apple's tutorial (https://developer.apple.com/library/ios/referencelibrary/GettingStarted/RoadMapiOS/ThirdTutorial.html#//apple_ref/doc/uid/TP40011343-CH10-SW1)
I have a text field that adds a new task to an array. When I press "done" the task is added to the array and it appears in the tableview - but then i get this in the debug:
[XYZViewController addTaskField:]: unrecognized selector sent to instance 0x7fb13348c8c0
2014-10-16 18:00:36.120 Tutorial123[7880:1837804] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[XYZViewController addTaskField:]: unrecognized selector sent to instance 0x7fb13348c8c0'
Here's the code I have on that file that handles the text field:
#import "XYZViewController.h"
#import "ToDoItem.h"
#interface XYZViewController ()
#property (weak, nonatomic) IBOutlet UITextField *addTaskField;
#property (weak, nonatomic) IBOutlet UIBarButtonItem *doneButton;
#end
#implementation XYZViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if(sender != self.doneButton) return;
if(self.addTaskField.text.length > 0){
self.todoItem = [[ToDoItem alloc] init];
self.todoItem.itemName = self.addTaskField.text;
self.todoItem.completed = NO;
}
}
#end
When you were wiring addTaskField button from storyboard did you create an action first then deleted the method in the code and created an outlet? If that's what happened you have to go back to storyboard click on the button and connection inspector and see that touch up inside doesn't have addTaskField: wired up. See image below. I added testAction: action. You have to press the little x next to View Controller testAction: to delete it. Let me know if that's what happened.

NSException with UIImagePickerController

I'm building my first IOS 7 iPad app in Xcode 5 - but I need some help with this issue.
I'm following this tutorial:
I don't quite understand what the writer means with:
"So open up testPickerViewController.h and we want to add in the following to the class reference."
UINavigationControllerDelegate, UIImagePickerControllerDelegate>
I got my view controller.h file here:
#import "ViewController.h"
#interface DetailViewController : ViewController <UINavigationControllerDelegate, UIImagePickerControllerDelegate>
{
UIImagePickerController *imgPicker;
IBOutlet UIImageView *customImage;
}
#property(nonatomic, retain)UIImagePickerController *imgPicker;
#end
my view controller.m file:
#import "DetailViewController.h"
#interface DetailViewController ()
#end
#implementation DetailViewController
#synthesize imgPicker, customImage;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
// Init the image picker
self.imgPicker = [[UIImagePickerController alloc]init];
self.imgPicker.allowsEditing = YES;
self.imgPicker.delegate = self;
self.imgPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
- (IBAction)AddImage:(id)sender {
// Let the user add an image for the specific subject
[self presentModalViewController:self.imgPicker animated:YES];
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editingInfo {
customImage.image = img;
[[picker parentViewController] dismissModalViewControllerAnimated:YES];
}
#end
I ran the app without doing what I wrote at the top, which resulted in an NSException in the main.m file.
What am I doing wrong here?
Edit
main.m
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
int main(int argc, char * argv[])
{
#autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
}
Edit
2014-05-17 14:56:47.509 myApp[1424:60b] * Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key image.'
Check the connection for the IBOutlet customImage in Interface Builder (IB) and make sure that the name of the referencing outlet and the UIImageView match. You can check by clicking on the Connection Inspector in IB (it's a little arrow inside of a circle).
In the tutorial that you posted a link to it shows the IBOutlet UIImageView declared as image. I'm assuming you changed the name of the IBOutlet and forgot to reconnect it. You can also check if the IBOutlet customImage is connected by checking if the dot beside the property declaration is filled or empty. Filled = connected and Empty = not connected.
Example of a property declaration for an IBOutlet:
#property (weak, nonatomic) IBOutlet UIImageView * customImage;
Also, <UINavigationControllerDelegate, UIImagePickerControllerDelegate> are protocols. By adding those lines to the #interface declaration you are, very simply, stating that, "I want my UIViewController to conform to the UINavigationControllerDelegate and the UIImagePickerControllerDelegate. I also want to implement my own code when certain events happen."

this class is not key value coding-compliant for the key image view

Debugger is showing this information:
2013-07-16 15:23:15.731 app2[2501:c07] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<UIViewController 0x71a4140> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key imageview.'
My app currently consist of two view controllers.
'ViewController' has just a button to start the app and pressing the button launches the new view.
ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
- (IBAction)startBtn:(id)sender;
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)startBtn:(id)sender {
UIViewController *flipViewController = [[UIViewController alloc] initWithNibName:#"AppViewController" bundle:[NSBundle mainBundle]];
[self presentViewController:flipViewController animated:YES completion:NULL];
}
#end
My second view controller 'AppViewController' has a UIImageView and a UIButton. It's code is:
AppViewController.h
#import <UIKit/UIKit.h>
#interface AppViewController : UIViewController
#property (strong, nonatomic) IBOutlet UIImageView *imageview;
#end
and
AppViewController.m
#import "AppViewController.h"
#interface AppViewController ()
#end
#implementation AppViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
The problem I am facing is that the moment I connect the property between the 'xib' file and 'h' file by Ctrl-dragging the image view into the 'h' file, my app crashes in the simulator and gives me the error.
I reckon this might be too simple of an error for many of you, however, I am unable to rectify it. I have read numerous posts on this forum and have tried most, including the #synthesize fix and the like.
Your flipViewController should be a AppViewController.
The issue is that AppViewController is a subclass of UIViewController so you are allowed to instantiate it as a UIViewController, but you lose access to all the additional properties that your AppViewController adds on top of UIViewController's.
Your button handler should look like:
- (IBAction)startBtn:(id)sender {
AppViewController *flipViewController = [[AppViewController alloc] initWithNibName:#"AppViewController" bundle:[NSBundle mainBundle]];
[self presentViewController:flipViewController animated:YES completion:NULL];
}

ios - tableView:numberOfRowsInSection ...NSInvalidArgumentException when working with UITableView

I have this header:
#interface MyBusinessesController : UIViewController
#property (weak, nonatomic) IBOutlet UITableView *businessList;
#property (weak, nonatomic) IBOutlet UILabel *loadingLabel;
#end
and I have this in my .m file:
#import "MyBusinessesController.h"
#interface MyBusinessesController ()
#end
#implementation MyBusinessesController
#synthesize businessList;
#synthesize loadingLabel;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
NSLog(#"HIDING 1");
// HIDE THE LIST UNTIL IT LOADS
self.businessList.hidden=TRUE;
NSLog(#"HIDING 2");
}
- (void)viewDidUnload
{
[self setLoadingLabel:nil];
[self setBusinessList:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#end
When this controller runs, both of the NSLOG lines execute, and the exception happens after that.
Here is the exception:
-[MyBusinessesController tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x6a8f330
2012-07-29 21:31:38.355 BusinessPlan[3973:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[MyBusinessesController tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x6a8f330'
*** First throw call stack:
(0x13d5022 0x1566cd6 0x13d6cbd 0x133bed0 0x133bcb2 0x1fd06b 0x1ff862 0xad66d 0xad167 0xb0134 0xb4623 0x5dd42 0x13d6e42 0x1d8d679 0x1d97579 0x1d1c4f7 0x1d1e3f6 0x1d1dad0 0x13a999e 0x1340640 0x130c4c6 0x130bd84 0x130bc9b 0x12be7d8 0x12be88a 0x1f626 0x1bfd 0x1b65)
Would anyone be able to help me understand what is going wrong here?
Thanks!
You've probably set up the MyBussinessViewController as the datasource in interface builder but you didn't set up the datasource methods in it. You can check by right clicking on the tableview in interface builder and seeing what the datasource field is connected to.
For reference you need:
tableView:numberOfRowsInSection:
numberOfSectionsInTableView:
tableView:cellForRowAtIndexPath:
http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableViewDataSource_Protocol/Reference/Reference.html
Happy coding.

IBAction for UIButton Causes an unrecognized selector sent to instance error (iOS)

I am having trouble using some UIButtons to call actions in my Tab Bar application.
My .h file:
#import <UIKit/UIKit.h>
#interface ContactViewController : UIViewController {
UIButton *callTollFreeButton;
UIButton *callLocalButton;
UIButton *emailButton;
}
#property (nonatomic, retain) IBOutlet UIButton *callTollFreeButton;
#property (nonatomic, retain) IBOutlet UIButton *callLocalButton;
#property (nonatomic, retain) IBOutlet UIButton *emailButton;
-(IBAction)callPhone:(id)sender;
-(IBAction)callTollFree:(id)sender;
-(IBAction)clickEmailButton:(id)sender;
#end
My .m file:
#import "ContactViewController.h"
#implementation ContactViewController
#synthesize callLocalButton,callTollFreeButton,emailButton;
-(IBAction)callPhone:(id)sender {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"tel:7576236600"]];
}
-(IBAction)callTollFree:(id)sender{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"tel:8003334645"]];
}
-(IBAction)clickEmailButton:(id)sender{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"mailto:email#email.com?subject=Hello"]];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)dealloc
{
[callLocalButton release];
[callTollFreeButton release];
[emailButton release];
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#end
My .m file:
The error message I get is (I get error message for any button clicked, this one is clicking the e-mail button in particular):
2011-11-19 18:39:38.786 Miller Tab Bar[761:207] -[UIViewController clickEmailButton:]: unrecognized selector sent to instance 0x6b29f40
2011-11-19 18:39:38.790 Miller Tab Bar[761:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIViewController clickEmailButton:]: unrecognized selector sent to instance 0x6b29f40'
I've tried to connect and reconnect the outlets and actions to the objects on the view controller. Is it possible that it crashes because the SDK can't run test phone calls or e-mails?
I typed 'po 0x6b328d0' into the debugger to find out what the problem object is and it came back as a UIViewController which possibly means that the View Controller is being released before the action is called? What would be causing that?
Thanks.
In your Nib or storyboard, it looks like your view controller instance is of type UIViewController instead of ContactViewController.
So select the view controller in your Nib (or storyboard), and change its type to ContactViewController.
Of course, if this view controller isn't being loaded from a nib or storyboard, then just check where you create it and make sure you created an instance of the correct class.
It is a bit bizarre. I think your viewcontroller isn't being set up properly.
The bizarre part is you shouldn't be able to set the actions if it is not set up properly (File Owner- ContactViewController)
When I send an unrecognized selector to a custom ViewController, it says
-[customViewController foo]: unrecognized selector sent to instance 0x6a2b440
It seems the xib's actual file owner is being instantiated as a UIViewController
Try changing:
UIButton *callTollFreeButton;
UIButton *callLocalButton;
UIButton *emailButton;
to
IBOutlet UIButton *callTollFreeButton;
IBOutlet UIButton *callLocalButton;
IBOutlet UIButton *emailButton;
And hook up the UILabels to their respective buttons. See if that helps.

Resources