Simple functions giving me a crash on Objective-C - iphone

I'm coding a simple calculator and I want to make an unique function for all 4 arithmetic buttons. I got here:
-(IBAction)simbolo1:(UIButton*)sender{
if (isNumeric(campo1.text)) {
NSString *str=campo1.text;
campo2.text=str;
int S=1;
NSString *cmp1 = [NSString stringWithFormat:#"%d", S];
sinal.text=cmp1;
campo1.text=#"";
} else {
campo1.text=#"Only numeric values";
}
}
But, for some reason, I cant get this to work. Everytime I click in the button, I get a crash.
So, I check where really was the error and I deleted the whole code:
-(void)simbolo1:(UIButton*)sender{
campo1.text=#"Only numeric values";
}
Those lines of code appear to gave me the same error as before
I'm getting a 'green :marker' on this file:
#import <UIKit/UIKit.h>
int main(int argc, char *argv[]){
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
int retVal = UIApplicationMain(argc, argv, nil, nil); <--------- This line right here
[pool release];
return retVal;
}
EDIT---------------
On the debud console im getting this:
2011-09-30 09:17:14.319 Teste iPhone[28432:fb03] Applications are expected to have a root view controller at the end of application launch
2011-09-30 09:17:21.714 Teste iPhone[28432:fb03] -[Teste_iPhoneAppDelegate simbolo1]: unrecognized selector sent to instance 0x6b7c2d0
2011-09-30 09:17:21.715 Teste iPhone[28432:fb03] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Teste_iPhoneAppDelegate simbolo1]: unrecognized selector sent to instance 0x6b7c2d0'
*** First throw call stack:
(0x166d062 0x17fed0a 0x166ecfd 0x15d3f10 0x15d3cf2 0x166eed9 0x16eb2 0x16e4a 0xbc3e6 0xbc8af 0xbbb6e 0x3c2a0 0x3c4c6 0x22c74 0x16399 0x1557fa9 0x16411d5 0x15a6042 0x15a492a 0x15a3dd4 0x15a3ceb 0x1556879 0x155693e 0x1438b 0x272d 0x26a5)
terminate called throwing an exception(gdb)

There is not enough information to help you. You need to look in the debugger console and show us the error and the stack.
However, I'm going to take a wild guess and say that you didn't connect the campo1 IBOutlet to its UIView in Interface Builder, and that it's nil. To fix, edit the view in Interface Builder (or in Xcode 4, just click on it), and drag the little circle next to the campo1 outlet (in the Connections tab) onto the UIView component that you want it to correspond to.
If that's not it, the error isn't here, but is probably caused by something else in your program.
[EDIT after seeing console]: Your connections look misconfigured. Why would it try to send the simbolo1 selector to Teste_iPhoneAppDelegate? It should send it to your ViewController. Did you play around with the connections (specifically, the delegate one) in Interface Builder -- or the class?

to set text in UITextField use
[campo1 setText:#"Only numeric values"];
and also don't forget to link IBOutlet UITextField* campo1; to proper UITextField in XCode IB

Related

exc_bad_access when Property added

I have a TabBarController in one tab I have a TableViewController inside a NavigationController.
When I add a property like
#property CGFloat rowHeight;
or add something like
if ([self.modelTypeCell isEqualToString:#"TypeOneCell"])
I get an
error: gdb remote returned an error: E08 EXC_BAD_ACCESS (Code=1....
in this line
#autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
I cant debug anything and no breakpoint get hit before the error occurs.
EDIT: Zombies didnt give me informations, in Instruments and on a real device no error was thrown...
EDIT2:
I found out the problem should lay here
-(void)setupControllerDictionaries
{
self.masterViewControllerDictionary = [[NSMutableDictionary alloc] init];
TableMasterViewController *master = [[TableMasterViewController alloc] initWithModelType:ModelTypeCustomer];
[self.masterViewControllerDictionary setObject:master forKey:[NSNumber numberWithInt:ModelTypeCustomer]];
[master release];
}
when I comment out the master release the error is gone, maybe its the error maybe its somewhere else
Property can be best used when written like this:
#property(nonatomic, assign) CGFloat rowHeight;
nonatomics is for that the property can perform in multiple threads and assign means that property should use assignment property while assigning to property something. The retain can not be used with this as this variable is not a pointer type

Which class must I extend?

(I am developing an app that presents chat messages in a table. But, this chat can't be started by the user, when the user receives a message the chat view opens. So, I made this code:
- (void) newMessageReceived:(NSMutableDictionary *)message
{
General *general = [General sharedManager];
NSString *firstmessage=[message objectForKey:#"msg"];
NSString *from=[message objectForKey:#"sender"];
NSArray *listItems = [from componentsSeparatedByString:#"#"];
NSString *fromsplit=[listItems objectAtIndex:0];
general.firstmess=firstmessage;
general.firstfrom=fromsplit;
NSLog(#"Mensaje recibido: %# de %#", [message objectForKey:#"msg"], fromsplit);
ChatViewController *cvc=[[ChatViewController alloc]initWithNibName:#"Chat" bundle:nil];
[[self navigationController]pushViewController:cvc animated:YES];
}
Everything is ok, until here. The ChatViewController extends UITableViewController. But, when a message is received i get the following exception:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UITableViewController loadView] loaded the "Chat" nib but didn't get a UITableView.
Then, i try to change the class extended to UIViewController (did this to check that the program enters the numberOfRowsInSection method) and then i receive:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[ChatViewController setTableViewStyle:]: unrecognized selector sent to instance 0x9863200'
I think that solving the first exception would fix my problem. Any help?
Thank you.
In the second exception i think you have called [self setTableViewStyle:] method, while
you have made it UIViewController.
So try to call this method by tableViewOutlet.
[tableView setTableViewStyle:];
Hope this will help you
Solved it. i click the .xib file, under "objects", clicked "View". And then, in the Identity Inspector (the third one, starting from the left), in Custom Class, set it to UITableView. It was simply "View" before. And then, everything worked fine.

string to another view

I've followed tutorial from
passing data between classes
did it, and is working fine, but I really need to send some string value (a date) to another view, (is simple I know but Im a noob for this!),
so my problem is that I need to send this string (Date), to the other view, the string is ok, but I dont seem to get (yet)how to construct the function,
I get no warnings but the app breaks,
- (void)calendarView:(KLCalendarView *)calendarView tappedTile:(KLTile *)aTile{
NSLog(#"Date Selected is %#",[aTile date]);
string1 = [[aTile date] description];
dateis.text = string1; //label to check string is working
NSLog(#"ahi va! %#", string1);
NSString *cucux = dateis.text;
CroTime *croco = [CroTime alloc];
croco.string1 = cucux;
[self.view addSubview:croco.view];
NSLog(#"croco = %#", cucux);
}
Console message
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[CroTime setString1:]: unrecognized selector sent to instance 0x5e2e1e0'
Thanks a lot!!
setString1 is a setter method which calls when you set a property value.So from your error this looks like you make a property string1 in class CroTime but you did not synthesize it.
This type of condition occur only in this case. so ensure that when you make property then synthesize it in .m file.

IBAction being called NSCFDictionary and crashing app

I have an NSObject set up to control various elements on my screen.
I initialize the class inside my View Controller like this:
self.pageSetupClass = [[PageSetup alloc] set:self.pageID];
In IB I have added my NSObject object and linked a button to an IBOutlet on it. I've also linked it to an IBAction found in the object. When I click this button the app crashes and I get this error:
2010-09-23 15:33:11.640 BookTest10[49139:207] *** -[NSCFDictionary clickSoundByte:]: unrecognized selector sent to instance 0x4b10bc0
2010-09-23 15:33:11.641 BookTest10[49139:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSCFDictionary clickSoundByte:]: unrecognized selector sent to instance 0x4b10bc0'
2010-09-23 15:33:11.642 BookTest10[49139:207] Stack: (
42195024,
43352876,
42203739,
41666166,
41662962,
2915566,
3413054,
3422400,
3417197,
3042792,
2934339,
2965976,
51188092,
41474204,
41470120,
51181725,
51181922,
2958194
)
terminate called after throwing an instance of 'NSException'
There is nothing in the function at the moment so it must be the calling that crashes it...right?
If you need me to post more code I certainly can. If you have any ideas I'm all ears. Thank You
Edit:
This is the contents of the function set called out when I initialize the PageSetup object:
-(PageSetup*) set:(int) i {
self = [super init];
if(self) {
self.iD=i;
self.pageSetupFile = [[NSBundle mainBundle] pathForResource:#"PageSetup" ofType:#"plist"];
self.pageSetupArray = [[NSMutableArray alloc] initWithContentsOfFile:self.pageSetupFile];
self.pageInfo = [self.pageSetupArray objectAtIndex:self.iD];
[self initializeSoundBytes];
[self initializeAnimations];
[self initializeToys];
NSLog(#"Page Setup Initializing: %#", self.pageInfo);
return self;
}
return self;
}
Another Edit:
I set my set function to return (void) and no longer initialize it using init.
I now receive this error message:
2010-09-23 17:10:05.096 BookTest10[50519:207] *** -[UITouch clickSoundByte:]: unrecognized selector sent to instance 0x4b1b580
2010-09-23 17:10:05.097 BookTest10[50519:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[UITouch clickSoundByte:]: unrecognized selector sent to instance 0x4b1b580'
2010-09-23 17:10:05.099 BookTest10[50519:207] Stack: (
42166352,
43324204,
42175067,
41637494,
41634290,
2886894,
3384382,
3393728,
3388525,
3014120,
2905667,
2937304,
51159420,
41445532,
41441448,
51153053,
51153250,
2929522
)
terminate called after throwing an instance of 'NSException'
Final Edit:
I had forgotten to connect my NSObject to the File's Owner in IB.
Could it be that when I reinitialize
this object...
Wait... what? In Objective-C you never call init* methods more than once.
In any case, the underlying problem appears to be a straightforward memory management issue. Something is being released before its time and it just so happens that a dictionary lands at that location. Turn on zombie detection and it'll likely catch the problem.
self.pageSetupClass = [[PageSetup alloc] set:self.pageID];
What does set: return? Is that a method of your PageSetup class? Does it return self?
Because apparently you're assigning pageSetupClass to whatever set: returns...
Unless set: returns self, you should try this:
self.pageSetupClass = [[PageSetup alloc] init];
[self.pageSetupClass set:self.pageID];
Edit:
ok, if you added the object in IB, the object was already instantiated for you when you loaded the nib, and the IBAction and IBOutlet connections have already been made.
When you do self.pageSetupClass = ..., you're setting pageSetupClass to a new instance, and that probably leaves the button with a bad target for its IBAction.
If the object was instantiated from the nib, and you have a reference to it in pageSetupClass from a IBOutlet, then just set whatever you want in the object, no need to alloc a new one.

how to solve unrecognized selector sent to instance?

I have a uitableview 'A' which has cells, on clicking one of the cell, it needs to push another table view 'B'. and now when a cell is clicked in 'B', its needs to open a simple view.
I have managed to display both the tables in two different views. but when a cell is clicked in the menu B. it hangs and shows the below message
> unrecognized selector sent to > instance
could someone please tell me how do i solve this.thanks
Please find below my code
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSInteger row = [indexPath row];
NSLog(#"entering the if loop libdecripviewcontoller");
if(self.libraryDescripViewController == nil){
NSLog(#"its creating new instance of libdecripviewcontoller");
LibraryDescripViewController *aLib = [[LibraryDescripViewController alloc] initWithNibName:#"LibraryDescripView" bundle:nil];
self.libraryDescripViewController = aLib;
[aLib release];
}
libraryDescripViewController.title = [NSString stringWithFormat:#"%#",[libraryMenu objectAtIndex:row]];
ULS1AppDelegate *delegate = [[UIApplication sharedApplication] delegate ];
[delegate.searchLibNavController pushViewController:libraryDescripViewController animated:YES];
}
Please find the output below
2010-06-27 20:13:15.521 ULS1[1020:207]
entering the if loop
libdecripviewcontoller 2010-06-27
20:13:15.533 ULS1[1020:207] its
creating new instance of
libdecripviewcontoller 2010-06-27
20:13:15.541 ULS1[1020:207] *
-[LibraryMenuTabViewController setLibraryDescripViewController:]:
unrecognized selector sent to instance
0x3c2ec70 2010-06-27 20:13:15.554
ULS1[1020:207] * Terminating app due
to uncaught exception
'NSInvalidArgumentException', reason:
'*** -[LibraryMenuTabViewController
setLibraryDescripViewController:]:
unrecognized selector sent to instance
0x3c2ec70' 2010-06-27 20:13:15.558
ULS1[1020:207] Stack: ( 29303899,
2512004361, 29685819, 29255286,
29107906, 14755, 3050054, 3033220,
287146, 29088448, 29084744, 37393941,
37394138, 2777007, 11184, 11038 )
You can get more information about where the error is happening by opening your debugger window, clicking on "Show Breakpoints", and setting the following two breakpoints:
[NSException raise]
objc_exception_throw
Now when your app is about to crash, the debugger will usually show you exactly what line of code is causing the problem.
For this reason, it's good practice to have these breakpoints on every app you are developing.
It seems that you are calling "self.libraryDescripViewController = aLib" which is another way of writing [self setLibraryDescripViewController:alib].
The most likely cause of this would be in that you haven't declared the method setLibraryDescripViewController in your class.
Hope this helps.
There is way too little information here to diagnose exactly whats causing this, but one thing is certain. The method you are sending to the object is not defined for that object. Please post the code thats causing the error and the .h and .m files for the simple view you are talking about.
You probably need to set the class of the view controller in IB to LibraryDescripViewController. Double click the LibraryDescripViewController.xib file in xcode and when IB opens, in the identity view of the file's owner set the class to LibraryDescripViewController