sender currentTitle not recognized in Xcode - iphone

I am doing something as simple as get the current title of an UIButton
-(IBAction)buttonPressed:(id)sender{
NSString *someString = [sender currentTitle];
}
For some reason Xcode confuses sender with self because the complete options only retunrns methods that belongs to "self". No matter whatever I do, I get the same
-(IBAction)buttonPressed:(id)sender{
UIButton *btn = (UIButton *)sender;
NSString *someString = [btn currentTitle];
}
Xcode crashes in NSString line with "unrecognized selector sent to btn". I can copy working code from another project and the same happens. What could be wrong?
SOLUTION
Resarted XCode and it worked fine o.O

I don't think Xcode is confusing sender with self, what's happening is that Xcode knows nothing about sender, since it was declared as id, which means Xcode only knows it's an object.
The suggestions you are getting in Xcode are probably just methods every object has.
You have to make sure that sender is what you expect, try doing this and checking if it's actually a button or something else:
-(IBAction)buttonPressed:(id)sender{
NSLog(#"Sender: %#", sender);
}

SOLUTION
Resarted XCode and it worked fine o.O

Possible reason for "unrecognized selector sent to btn" is when you call a method which was not actually written. Or you might be called
[self buttonPressed];
instead of
[self buttonPressed:];

Your problem is your calling the 'id' not the button its self. Replace the 'id' with 'UIButton' like so...
-(IBAction)buttonPressed:(UIButton *)sender{
NSString *someString = sender.currentTitle;
}

I think the problem is wrong syntax
It is
sender.currentTitle;
OR
btn.currentTitle;
currentTitle is a property and there is no function called currentTitle and hence you get the error "unrecognized selector sent to btn".

Related

App crash in button

I have face strange problem on UIButton.
When i tap button the app is crash .
I wrote below code for that...
-(IBAction)renameTest:(id)sender
{
NSLog(#"Tapped");
// UIButton *button = (UIButton *)sender;
NSUInteger row = 1;//button.tag;
NSString * titlename = [titleArray objectAtIndex:row];
RenameTest *renameVC = [[RenameTest alloc]initWithNibName:#"RenameTest" bundle:nil];
renameVC.titlespell = titlename;
NSLog(#"titlespell = %#",renameVC.titlespell);
NSLog(#"title = %#",titlename);
// [button release];
[self.navigationController pushViewController:renameVC animated:YES]; //here APP is cresh
[renameVC release];
}
I check also my .Xib file name .It is ok and files are there.
error msg is below :
2012-07-11 14:28:29.079 TestApp[238:207] -[__NSCFDictionary _isNaturallyRTL]: unrecognized selector sent to instance 0x73d8a80
Thanks in Advance.
[button release] is causing the problem. Remove it and check.
_isNaturallyRTL is an NSString method (private), and it looks like you are passing a dictionary instead of a string somewhere.
Breaking on the exception and showing us the call stack at that point would help tremendously.
If u have created the button in xib file then u cannot release it because you have not allocated it and claimed ownership.. U should call release only on objects you have allocated by calling alloc..
Remove the [button release] statement .. that should fix the crash!
You have a crash which is related to a dictionary and your titlename string is set equal to, titleArray objectAtIndex:row. I believe, without seeing the declaration of your variables, that titleArray is a dictionary, or is a NSMutableArray importing from a plist of dictionaries, either way you need to use objectForKey, when using dictionaries, like this:
[[titleArray objectAtIndex:(NSUInteger *)] objectForKey:(NSString *)]
Obviously replace (NSUInteger *) with your integer row and (NSString *) with the name of your key. This may not be the answer but from your crash report and visible code, this is what I assume.

Programmatically adding switch to scrollview throws exception

I am using storyboards and have dragged a scrollview window onto a view. In my code I am programmatically creating a switch object that is somehow not being initialized correctly. The switch appears on the view correctly but whenever I click the switch, an exception is thrown saying
"unrecognized selector sent to instance 0x6a786f0'"
I have also attempted to edit the On/Off text to Yes/No and accessing the switch also throws the same exception. Clearly I have missed something in creating my switch and setting the correct delegates or whatever.
My code to create the switch is..
UISwitch *switchControl = [[UISwitch alloc] initWithFrame:CGRectMake(x, y, 60, 20)];
[switchControl addTarget:inputsView action:#selector(actionSwitch:) forControlEvents:UIControlEventTouchUpInside];
[switchControl setBackgroundColor:[UIColor clearColor]];
//[(UILabel *)[[[[[[switchControl subviews] lastObject] subviews]
// objectAtIndex:1] subviews] objectAtIndex:0] setText:#"Yes"];
//[(UILabel *)[[[[[[switchControl subviews] lastObject] subviews]
// objectAtIndex:1] subviews] objectAtIndex:1] setText:#"No"];
[inputsView addSubview:switchControl];
inputsView is the name of my UIScrollView that I created in my .h file.
I should note, when the exception is called on clicking the switch, in the error the 'reason' is reason: '-[UIScrollView actionSwitch:]. When the error is called by trying to adjust the text, the 'reason' is reason: '-[UIImageView setText:]
Any help on what I am missing would be great.
Thanks
The exception is correct, UIScrolView does not have a method actionSwitch:. The target parameter in addTarget: is the object you want to receive the selector: argument.
If your posted code is in the class that has the actionSwitch: method then you would use self as the target, like so:
[switchControl addTarget:self action:#selector(actionSwitch:) forControlEvents:UIControlEventTouchUpInside];
And as a side note. For a UISwitch you generally want your method called for UIControlEventValueChanged, that way if the user just touches the switch but doesn't "switch" it your method won't be called.
Edit in response to: "I just tried changing to 'self' for the UISwitch and the error still occurs. I haven't created an actionSwitch method."
Yes, your application would still crash because whatever you pass in as the target must implement the selector/method passed in as the selector.
The view controller is the ideal place to implement this method. A very standard implementation of this event target would look like:
-(void)actionSwitch:(UISwitch *)theSwitch{
if (theSwitch.isOn){
// Switch was switched on respond accordingly
}
else {
// Switch was switched off respond accordingly
}
}

Calling function in xcode iPhone

If I have this function for button click
-(IBAction)loginButton:(UIButton *)sender{ }
How can I call this button function from this another method
-(void)increaseAmount {
myNumber = myNumber+0.01;
if (myNumber >= 1) {
[self loginButton:sender];
}
progressViewAuto.progress = myNumber;}
I have tried the above code but getting error that USE OF UNDECLARED IDENTIFIER 'sender'
Simply pass in nil or even self.
And, if you want to be perfectly correct, you can link your UIButton to IBOutlet UIButton* theLoginButton; and then say [self loginButton:theLoginButton];.
But this would only be needed if you actually reference the sender in your loginButton routine, and that's the exception rather than the rule.
You probably just want to send nil, like this:
[self loginButton:nil];

UITextField settext not working

This is frustrating the hell out of me. I am a beginner programmer and cannot figure out why the text is not being changed.
Here is my method which is supposed to set the text of a UITextField:
-(void)updateDays:(NSInteger)days
{
NSString* daysString = [[NSString alloc] initWithFormat:#"%d", days];
[daysTextField setText:daysString];
[daysString release];
}
For whatever reason nothing is happening.
Help appreaciated!
Anytime you have a frustration along the lines of "why isn't this line working", use the debugger or just add an NSLog before it to print out the relevant data:
NSLog(#"updateDays: %# %# <= %#", daysTextField, daysTextField.text, daysString);
Then you know the line (a) is getting executed, (b) the variables are the ones you think they are, and (c) the values are reasonable.
I have experienced this several times, but I think all of them was reasoned by that the UITextField was not initialized at that point.
Set a breakpoint and use debugger to make sure that your UITextField is not nil. You should also check the connection between the .xib file and your code.
Check if the method is called. If yes check if the textfield is set up properly. Cross check to see if the IBOutlet connections are made to the correct object.
I meet this problem too .
It's my code:(in viewdidload)
UITextView *tv = [[UITextView alloc]initWithFrame:CGRectMake(0,0,320,50)]];
tv.text = #"12345";
tv.textColor=[UIColor blackColor];
[self.view addSubView:tv];
And the text(12345) doesn't show;
Lastly,I found that when i set textColor to gray,or any color not black,it works;
I think that it's a bug of the simulator.
I use xcode 4.2 and the iphone5.0simulator.

Unknown problem in my code - Console says: unrecognized selector sent to instance - I try to use the MGTwitterEngine

since yesterday I've got a bug in my application and I don't get where it is. Actually I am pretty sure that I did not change anything and that it worked perfectly yesterday.
I do not intent to publish all of the code but I can post my first ViewController, if you want.
The problem occurs in both ViewControllers. I use the MGTwitterEngine-API to send a message to Twitter. As far as I know the bug causing a crash is located in the following line.
MGTwitterEngine *twiit = [[MGTwitterEngine alloc] initWithDelegate:self];
[twiit setUsername:usernamee password:passworde];
I thought there is nothing wrong about it. Am I wrong?
usernamee and passworde are NSStrings.
The console returns:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[MGTwitterEngine setUsername:password:]: unrecognized selector sent to instance 0x3911fb0'
If there are questions or if it's impossible to solve the problem like that i would agree to send my hole code to somebody for check. It is not very complex and it just has to Views. Stil I just send the code to trustworthy persons with many reputations.
In addition I am sure that there is nothing wrong about the MGTwitterEngine since I did not ever change its code. It worked probably and I even copied a fresh MGTwitterEnginge into the project's folder.
Oh and here the UIAction, which reacts on a button and let the app crash:
- (IBAction) post: (id) sender{
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *usernamee;
usernamee = [prefs stringForKey:#"name_preference"];
//everthing works great, I checked, that usernamee got the right Nsstring from the preference
NSString *passworde;
passworde = [prefs stringForKey:#"password_preference"];
MGTwitterEngine * twitter1 = [[MGTwitterEngine alloc] initWithDelegate:self];
[twitter1 setUsername:usernamee password:passworde];
NSLog(#"sendUpdate: connectionIdentifier = %#", [twitter1 sendUpdate:[#""stringByAppendingString:twittermessage.text]] ); //It is not important, if I use sendUpdate, getDirectMessagesSinceID and so on...
loadingActionSheet = [[UIActionSheet alloc] initWithTitle:#"Posting to Twitter..." delegate:nil
cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
[loadingActionSheet showInView:self.view];
}
This code works fine for me. Are you getting any compiler warnings about the "unrecognized selector"?
It's possible your preferences do not actually contain the values you asked for - in which case it will not have returned strings, and you could be getting the NSInvalidArgument exception.
You can check this:
if (![passworde isKindOfClass:[NSString class]] || ![usernamee isKindOfClass:[NSString class])
{
NSLog(#"Some problem with your preferences");
}
isKindOfClass Documentation