Application crashes when UIActionSheet is called - objective-c

I am writing an app for iPad. In this app I have to use a Share button on the navigation bar on all the screens. I have applied the Share button on my Home class. I wrote the following code to apply button on the navigation bar:
CGRect frame5 = CGRectMake(835.0, 0.0, 40.0, 40.0);
UIImage *buttonImage5 = [UIImage imageNamed:#"ShareIcon.png"];
UIButton *ShareButton = [UIButton buttonWithType:UIButtonTypeCustom];
ShareButton.frame = frame5;
[ShareButton setBackgroundImage:buttonImage5 forState:UIControlStateNormal];
ShareButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
ShareButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
[ShareButton addTarget:self action:#selector(actionSheet) forControlEvents:UIControlEventTouchUpInside];
[ShareButton setBackgroundColor:[UIColor clearColor]];
[self.navigationController.navigationBar addSubview:ShareButton];
The share button is used to Email or print a screen. For this purpose I have used UIActionSheet. I wrote the following method to apply UIActionSheet:
-(void)actionSheet:(id)sender
{
UIActionSheet *popup = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:#"Email", #"Print", nil];
[popup showFromRect:CGRectMake(720.0, 3.0, 40.0, 40.0) inView:[self view] animated:TRUE];
[popup showFromBarButtonItem:(UIBarButtonItem *)sender animated:TRUE];
[popup release];
}
This method is applied on the Share button. When I build and run my app, there is no error or warning, i.e. the build is successful. But when I click on the share button the application crashes and it shows following in the debugger console window:
[Session started at 2011-10-21 21:26:58 +0530.]
2011-10-21 21:27:03.895 NewBostonEndoscopy[1351:207] -[Home actsht]: unrecognized selector sent to instance 0x6227010
2011-10-21 21:27:03.898 NewBostonEndoscopy[1351:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Home actsht]: unrecognized selector sent to instance 0x6227010'
I am not getting why this is happening. Any guess?
Any help will be highly appreciated.
Thanks and regards
Prateek

You are showing the popup twice, I'm not sure if this is the reason for you crash, but try removing the line
[popup showFromRect:CGRectMake(720.0, 3.0, 40.0, 40.0) inView:[self view] animated:TRUE];

[ShareButton addTarget:self action:#selector(actionSheet) forControlEvents:UIControlEventTouchUpInside];
change to:
[ShareButton addTarget:self action:#selector(actionSheet:) forControlEvents:UIControlEventTouchUpInside];
The missing ':' after actionSheet is the culprit.

As the log says, you are calling -actsht on Home, which apparently does not implement a method with this name. Try searching your document for actsht and see what you meant.
Probably, you used it in a UIBarButtonItem initializer, e.g. -initWithBarButtonSystemItem:target:action:, and specified a selector for action that does not exist. The compiler would not notice this.

Related

Unrecognized Selector sent to Instance in another class

I've been trying to figure out something that might be obvious and I'm missing the point or losing it. Is something wrong with my toggleStormButtons function that XCode isn't seeing it?
In my main class I have the following to call a function in another class:
STopLeftMenu *mTopLeft = [[STopLeftMenu alloc]init];
[mTopLeft drawStormToggleButton];
Then in the other class I have 2 functions:
- (void)toggleStormButtons{
[UIButton animateWithDuration:0.50 animations:^{
if (stormToggleBtn.transform.tx == 0){
[stormToggleBtn setTransform:CGAffineTransformMakeTranslation(307, 0)];
UIImage* hideButtonImg = [UIImage imageNamed:#"aiga_right_arrow_mod_hide_resize.png"];
[stormToggleBtn setBackgroundImage:hideButtonImg forState:UIControlStateNormal];
}
else{
[stormToggleBtn setTransform:CGAffineTransformMakeTranslation(0, 0)];
UIImage* showButtonImg = [UIImage imageNamed:#"aiga_right_arrow_mod_show_resize.png"];
[stormToggleBtn setBackgroundImage:showButtonImg forState:UIControlStateNormal];
}
}];
for(UIView* storm in stormButtonSaves){
[UIView animateWithDuration:0.50 animations:^{
if (storm.transform.tx == 0){
[storm setTransform:CGAffineTransformMakeTranslation(307, 0)];
storm.alpha = .65;
}
else{
[storm setTransform:CGAffineTransformMakeTranslation(0, 0)];
storm.alpha = 0;
}
}];
}
}
- (void)drawStormToggleButton{
//Storm Pullout Toggle Button
UIImage *buttonImageNormal = [UIImage imageNamed:#"aiga_right_arrow_mod_show_resize.png"];
stormToggleBtn = [[UIButton alloc] initWithFrame:CGRectMake(10, 10, 65, 75) ];
[stormToggleBtn setBackgroundImage:buttonImageNormal forState:UIControlStateNormal];
stormToggleBtn.backgroundColor = [UIColor clearColor];
stormToggleBtn.alpha = 0.5;
[stormToggleBtn addTarget:self action:#selector(toggleStormButtons) forControlEvents:UIControlEventTouchUpInside];
[viewsToRemove addObject:stormToggleBtn];
[mv addSubview:stormToggleBtn];
}
I seem to be getting an unrecognized selector message:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSMallocBlock__ toggleStormButtons]: unrecognized selector sent to instance 0x7bc9ca0'
*** First throw call stack:
(0x1b9e012 0x1966e7e 0x1c294bd 0x1b8dbbc 0x1b8d94e 0x197a705 0x8ae2c0 0x8ae258 0x96f021 0x96f57f 0x96e6e8 0x8ddcef 0x8ddf02 0x8bbd4a 0x8ad698 0x2599df9 0x2599ad0 0x1b13bf5 0x1b13962 0x1b44bb6 0x1b43f44 0x1b43e1b 0x25987e3 0x2598668 0x8aaffc 0x2285 0x2185)
libc++abi.dylib: terminate called throwing an exception
It sounds like your STopLeftMenu is being deallocated too early. The button does not retain its target, so you'll need to keep this object around as long as it needs to respond to the button's messages. If you're not sure how the object is getting deallocated, try debugging with Instruments.
I don't see anything wrong with the code you have shown. I tried it in an existing app of mine and while I had to a) declare a UIButton local variable, b) change the image used for the button and c) comment out the stuff in toggleStormButtons, the method was called every time I tapped the button, no problems.
You don't show your storage for the button. Are you using ARC? Is the button strong? If ARC it should be strong. If not ARC and you are not using a property to assign with a retain, that could cause problems.
What does viewsToRemove do? Looks like an array but it could be something else.
Why don't you use + buttonWithType: and set the frame later?

How to create a action to a button in Cocoa programmatically?

I'm trying to create a button in Cocoa in mac programmatically, but I don't know how to put a action on this, I'm trying like this:
NSRect frame = NSMakeRect(10, 200, 80, 100);
NSButton *btn = [[NSButton alloc]initWithFrame:frame];
[btn setButtonType:NSMomentaryPushInButton];
[btn setBezelStyle:NSRoundedBezelStyle];
[btn setTitle:#"Click me"];
[btn setAction:#selector(hideLabels:)];
[view addSubview:btn];
but the line [btn setAction:#selector(hideLabels:)]; don't work, how can I create a action here?
the method hideLabels is in function, because I used it with another button.
Does your hideLabels method take an argument?
if not, leave off the : from the selector

UIButton calling selector crashes the app

In my app I've got a button which is an instance variable declared in the .h file already.
Now after allocating it an assigning at target to it and actually pressing the button, the app crashes giving me following output in the debugger:
-[__NSCFString handleActionSheet:]: unrecognized selector sent to instance 0x9090330
2012-05-04 17:51:02.646 financeAppV2[2595:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString handleActionSheet:]: unrecognized selector sent to instance 0x9090330'
*** First throw call stack:
(0x13d1022 0x1562cd6 0x13d2cbd 0x1337ed0 0x1337cb2 0x13d2e99 0x1e14e 0x1e0e6 0xc4ade 0xc4fa7 0xc3d8a 0x2dfa1a 0x13a599e 0x133c640 0x13084c6 0x1307d84 0x1307c9b 0x12ba7d8 0x12ba88a 0x1b626 0x29dd 0x2945)
terminate called throwing an exceptionCurrent language: auto; currently objective-c
My code looks like following:
saveButton=[UIButton buttonWithType:UIButtonTypeCustom];
[saveButton setBackgroundImage:[UIImage imageNamed:#"intervalbutton"] forState:UIControlStateNormal];
[saveButton setBackgroundImage:[UIImage imageNamed:#"intervalbuttonpressed"] forState:UIControlStateHighlighted];
saveButton.alpha=.8;
saveButton.frame= frame;
UILabel* label=[[UILabel alloc]initWithFrame:CGRectMake(0, 2, saveButton.frame.size.width, 21)];
[saveButton addSubview:label];
[saveButton addTarget:self action:#selector(handleActionSheet:) forControlEvents:UIControlEventTouchDown];
[self.view addSubview:saveButton];
-(void)handleActionSheet:(id)sender{
NSLog(#"working");
}
Any ideas why the app could be crashing?
Without seeing the rest of your code it's difficult to be specific, but self is not correctly being retained.
You set up your button correctly (as far as I can see) and point the target to self. Unfortunately, by the time you press the button self has gone out of scope and the same memory is used by an NSString. iOS gets surprised when you send a string the handleActionSheet: message.
You're sending a message named initAllIVars: to an __NSCFString object (likely an NSString).
Do a search for that message and try to figure out why the object receiving the message is an NSString.
Just tested your code and it works. Maybe the frame it's not properly set
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIButton *saveButton = [[UIButton alloc] init];
saveButton=[UIButton buttonWithType:UIButtonTypeCustom];
[saveButton setBackgroundImage:[UIImage imageNamed:#"intervalbutton"] forState:UIControlStateNormal];
[saveButton setBackgroundImage:[UIImage imageNamed:#"intervalbuttonpressed"] forState:UIControlStateHighlighted];
saveButton.alpha=.8;
saveButton.frame= CGRectMake(5, 5, 200, 40);
UILabel* label=[[UILabel alloc]initWithFrame:CGRectMake(0, 2, saveButton.frame.size.width, 21)];
label.text = #"Hello";
[saveButton addSubview:label];
[saveButton addTarget:self action:#selector(handleActionSheet:) forControlEvents:UIControlEventTouchDown];
[self.view addSubview:saveButton];
}
-(void)handleActionSheet:(id)sender{
NSLog(#"working");
}

Selector is not called

I can't understand why the selector is not called.
//EDITED
[self.scrollView setContentSize:CGSizeMake(320, 600)];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn addTarget:self
action:#selector(validateTextFields:)
forControlEvents:UIControlEventTouchUpInside];
[btn setTitle:#"Продължи" forState:UIControlStateNormal];
btn.frame = CGRectMake(55, 580, 210, 50);
[self.scrollView addSubview:btn];
-(IBAction)validateTextFields:sender
{
NSLog(#"Called");
}
When I touch the button "Called" is not logged in console. If I change UIControlEventTouchUpInside to UIControlEventTouchDown validateTextFields method is executed.
You are adding a button on UIScrollView, this might be creating problem. Please check this question: iPhone: adding button to scrollview makes button inaccessible to interaction. It may be helpful.
The type for action should be
- (IBAction)validateTextFileds:(id)sender
{
// do your stuff
}
and your #selector parameter in addTarget should look like this: #selector(validateTextFields:)
try this:
[addContact addTarget:self action:#selector(buttonPressed:)forControlEvents:UIControlEventTouchUpInside];
-(IBAction)buttonPressed:(id)sender{
}

UIButton addTarget: self ... causes "Does not recognize selector" error

I initialised my UIButton-deriver liek this:
Button * it = [[Button alloc] initWithFrame:CGRectMake(x, y, image.size.width, image.size.height)];
Then, I do the next:
[(UIButton *)self addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[view addSubview:self];
The first line causes "Does not recognize selector" error.
Selector buttonClicked: looks like this:
-(IBAction) buttonClicked:(id)sender {
if (action) action();
else NSLog(#"Clicked.\n");
}
What am I doing wrong?
Add the action with:
[it addTarget:self
action:#selector(buttonClicked:)
forControlEvents:UIControlEventTouchUpInside];
and then add the button with
[view addSubview:it];
And don't create UIButtons with init... use the class method + buttonWithType:.
Be careful to to subclass UIButton, I am not sure if you really want this. Have a look at create uibutton subclass.
You create buttons with +(id)buttonWithType:(UIButtonType)buttonType