07-23-2011, 07:17 PM
#1 (permalink )
Registered Member
Join Date: Oct 2010
Posts: 1,210
UISegmentedControl selectedSegmentIndex Crash
I am using this code to replaceanobjectatindex in an array based upon the segment picked but it shows a really odd error in the console which I posted at the bottom.
Here is the code:
Code:
UIImage *buttonImage = [pictureButton imageForState:UIControlStateNormal];
//Convert UIImage to NSData
NSData *image = UIImagePNGRepresentation(buttonImage);
NSDictionary *accountInfo = [NSDictionary dictionaryWithObjectsAndKeys:
Name.text, @"Name",
image, @"ProfileImage", nil];
NSUInteger tempNum = accountSC.selectedSegmentIndex -1;
NSLog(@"tempNum:%d", accountSC.selectedSegmentIndex);
[self.accounts replaceObjectAtIndex:tempNum withObject:accountInfo]; NSLog(@"tempNum2 called");
//NSLog(@"accounts 2: %d", [self.accounts count]);
[[NSUserDefaults standardUserDefaults] setObject:self.accounts forKey:@"AccountArray"];
It crashes on the bolded line.
Here is the console message:
Code:
*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM replaceObjectAtIndex:withObject:]: index 4294967295 beyond bounds [0 .. 1]'
*** First throw call stack:
That number is ridiculous. Someone said that it is because a segment is not selected.
In my UI it looks like it is but this NSLog:
Code:
NSLog(@"tempNum:%d", accountSC.selectedSegmentIndex);
Prints out 0 even though in my UI, the selected segment is 0. Any idea why this is?
07-23-2011, 07:35 PM
#2 (permalink )
Just helping out.
Join Date: Feb 2011
Posts: 2,565
The selected index starts at 0. So if the first tab is selected it'll print out 0. If nothing is selected, the selected index is -1. Remove the -1 in your tempNum because if it's 0 it'll go -1 and obviously that's not a valid index in an array.
__________________
If you are looking for a quality developer, I'm your man. Give me a PM if you are interested.
New app - See screenshots and details at
www.globaclock.com .
If you want to thank me, click the link. Every click counts. If you want to do more, buy my app. A link is available on my website. Thanks.
07-23-2011, 11:37 PM
#3 (permalink )
Registered Member
Join Date: Oct 2010
Posts: 1,210
Ok so I do this now:
Code:
UIImage *buttonImage = [pictureButton imageForState:UIControlStateNormal];
//Convert UIImage to NSData
NSData *image = UIImagePNGRepresentation(buttonImage);
NSDictionary *accountInfo = [NSDictionary dictionaryWithObjectsAndKeys:
Name.text, @"Name",
image, @"ProfileImage", nil];
[self.accounts replaceObjectAtIndex:accountSC.selectedSegmentIndex withObject:accountInfo];
[[NSUserDefaults standardUserDefaults] setObject:self.accounts forKey:@"AccountArray"];
And I suppose that works, but it causes another crash to go off.
This code is fired whenever the text inside my textField changes.
Here is the code:
Code:
-(void)textFieldDidChange {
NSLog(@"SelectedSegment1:%d", accountSC.selectedSegmentIndex);
if (accountSC.numberOfSegments >= 1) {
[accountSC setTitle:Name.text forSegmentAtIndex:accountSC.selectedSegmentIndex];
}
if ([Name.text length] == 0) {
//NSLog(@"Name.text = '' here");
UIImage *btnImage = [UIImage imageNamed:@"Done-Button-Disabled.png"];
[doneButton setImage:btnImage forState:UIControlStateNormal];
[doneButton setEnabled:NO];
}
if (([pictureButton imageForState:UIControlStateNormal] == profilePicture) && ([Name.text length] >= 1)) {
//NSLog(@"2 if called");
UIImage *btnImage = [UIImage imageNamed:@"Done-Button.png"];
[doneButton setImage:btnImage forState:UIControlStateNormal];
[doneButton setEnabled:YES];
[accountSC setEnabled:YES];
}
NSLog(@"SelectedSegment2:%d", accountSC.selectedSegmentIndex);
}
This is what the console shows:
Code:
*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 4294967295 beyond bounds [0 .. 1]'
A similar type of crash with a crazy number. Anything wrong with the code I posted?
Edit: There is no objectAtIndex even in this method but it only changes when I type something in my textField so I am not sure what is going on.
Last edited by Objective Zero; 07-24-2011 at 10:59 AM .
Reason: Updated with NSLogs
07-24-2011, 01:20 AM
#4 (permalink )
Just helping out.
Join Date: Feb 2011
Posts: 2,565
NSLog accountSC.selectedSegmentIndex.
__________________
If you are looking for a quality developer, I'm your man. Give me a PM if you are interested.
New app - See screenshots and details at
www.globaclock.com .
If you want to thank me, click the link. Every click counts. If you want to do more, buy my app. A link is available on my website. Thanks.
07-24-2011, 11:01 AM
#5 (permalink )
Registered Member
Join Date: Oct 2010
Posts: 1,210
Its printing out this:
Code:
2011-07-24 12:00:25.266 App[2009:707] SelectedSegment1:-1
So thats obviously a problem but I am not sure why it is making the selected segment -1.
Edit: I added this line of code:
Code:
[accountSC setSelectedSegmentIndex:index];
And it seems to stop the crash but it does not load my Info/Image in my textfield or UIButton depending on the segment selected.
Edit2: So now the problem changed a bit. Like I said I am changing the info in these objects but the problem is, it seems to not load the correct items. The first object in my nsmutablearray should show when I click the first segment in my segmented control but it does not. I will post code if necessary.
Last edited by Objective Zero; 07-24-2011 at 01:13 PM .
07-24-2011, 01:26 PM
#6 (permalink )
Just helping out.
Join Date: Feb 2011
Posts: 2,565
If it's negative one and you are not modifying it in anyway it means there is no currently selected segment. I'd assign a selected segment right after creating the segment so this doesn't happen.
__________________
If you are looking for a quality developer, I'm your man. Give me a PM if you are interested.
New app - See screenshots and details at
www.globaclock.com .
If you want to thank me, click the link. Every click counts. If you want to do more, buy my app. A link is available on my website. Thanks.
07-24-2011, 02:00 PM
#7 (permalink )
Registered Member
Join Date: Oct 2010
Posts: 1,210
Yea that's what I ended up doing. The problem I am now encountering is the edit2 which I can post code for if necessary.
07-24-2011, 02:01 PM
#8 (permalink )
Just helping out.
Join Date: Feb 2011
Posts: 2,565
Post the code that is executed when a different segment is clicked.
__________________
If you are looking for a quality developer, I'm your man. Give me a PM if you are interested.
New app - See screenshots and details at
www.globaclock.com .
If you want to thank me, click the link. Every click counts. If you want to do more, buy my app. A link is available on my website. Thanks.
07-24-2011, 02:29 PM
#9 (permalink )
Registered Member
Join Date: Oct 2010
Posts: 1,210
Code:
- (void)updateInfo {
[self saveInfo];
if ([self.accounts objectAtIndex:accountSC.selectedSegmentIndex] != nil) {
NSDictionary *dict = [self.accounts objectAtIndex:accountSC.selectedSegmentIndex];
//NSString *name = [dict objectForKey:@"Name"];
NSString *name = [accountSC titleForSegmentAtIndex:accountSC.selectedSegmentIndex];
[Name setText:name];
NSData *imageData = [dict objectForKey:@"ProfileImage"];
UIImage *imageProfile = [UIImage imageWithData:imageData];
[pictureButton setImage:imageProfile forState:UIControlStateNormal];
}
else {
Name.text = nil;
[pictureButton setImage:nil forState:UIControlStateNormal];
}
}
- (void)saveInfo {
UIImage *buttonImage = [pictureButton imageForState:UIControlStateNormal];
//Convert UIImage to NSData
NSData *image = UIImagePNGRepresentation(buttonImage);
NSDictionary *accountInfo = [NSDictionary dictionaryWithObjectsAndKeys:
Name.text, @"Name",
image, @"ProfileImage", nil];
[self.accounts replaceObjectAtIndex:accountSC.selectedSegmentIndex withObject:accountInfo];
[[NSUserDefaults standardUserDefaults] setObject:self.accounts forKey:@"AccountArray"];
}
07-24-2011, 08:24 PM
#10 (permalink )
Registered Member
Join Date: Oct 2010
Posts: 1,210
I added this code:
Code:
NSDictionary *dict = [self.accounts objectAtIndex:0];
NSLog(@"nslog1:%@", [dict objectForKey:@"ProfileImage"]);
NSData *imageData = [dict objectForKey:@"ProfileImage"];
UIImage *imageProfile = [UIImage imageWithData:imageData];
[image1 setImage:imageProfile];
NSDictionary *dict2 = [self.accounts objectAtIndex:1];
NSLog(@"nslog2:%@", [dict2 objectForKey:@"ProfileImage"]);
NSData *imageData2 = [dict2 objectForKey:@"ProfileImage"];
UIImage *imageProfile2 = [UIImage imageWithData:imageData2];
[image2 setImage:imageProfile2];
So I can see if the images are different and in the end, it seems the problem is with the objectForKey.
This is the console log...
Code:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFConstantString objectForKey:]: unrecognized selector sent to instance 0x2cc08'
I also NSLogged the self.accounts count and it is 2 before this code is executed.
Thread Tools
Display Modes
Linear Mode
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
» Advertisements
» Online Users: 406
17 members and 389 guests
7twenty7 , blasterbr , buggen , chiataytuday , Clouds , dre , fiftysixty , HemiMG , jeroenkeij , jimmyon122 , jonathandeknudt , LEARN2MAKE , n00b , nyoe , pungs , tymex , UMAD
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,668
Threads: 94,121
Posts: 402,901
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jonathandeknudt