I have this situation:
// CustViewController.h
...
IBOutlet UISegmentedControl *sgmController;
...
@property (nonatomic, retain) IBOutlet UISegmentedControl *sgmController;
// CustViewController.m
@synthesize sgmController;
- (IBAction)apriRassegnePrecedenti {
NSString *model;
NSString *nibToLoad;
UIBarButtonItem *backBarButtonItem;
VecchieRassegneViewController *vecchieRassegne;
if ( self.sgmRassegna.selectedSegmentIndex == 1 ) {
backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Rass. odierne" style:UIBarButtonItemStylePlain target:self action:nil];
model = [NSString stringWithString:[[UIDevice currentDevice] model]];
if ( [model isEqualToString:@"iPhone"] || [model isEqualToString:@"iPhoneSimulator"] ) {
nibToLoad = [NSString stringWithString:@"VecchieRassegneViewController"];
} else {
nibToLoad = [NSString stringWithString:@"VecchieRassegneViewControllerPAD"];
}
vecchieRassegne = [[VecchieRassegneViewController alloc] initWithNibName:nibToLoad bundle:nil];
self.sgmRassegna.selectedSegmentIndex = 0; // The Crash comes here.
[self.navigationController pushViewController:vecchieRassegne animated:YES];
}
[backBarButtonItem release];
[vecchieRassegne release];
}
The IBOutlet is properly linked, I can't figure why it gives the error. Some hints?
EDIT: I've done ssame operations in another app and it works perfectly; I still can't figure what is causing the crash. I'll post the complete function. As said in the answer, the event is handled on "Value Changed". This is the test function that works:
- (IBAction)开发者_运维技巧pushDiTest {
ASecondView *secondView;
secondView = [[ASecondView alloc] initWithNibName:@"ASecondView" bundle:nil];
if ( self.sgmTest.selectedSegmentIndex == 1 ) {
self.sgmTest.selectedSegmentIndex = 0;
[self.navigationController pushViewController:secondView animated:YES];
}
[secondView release];
}
I assume the (IBAction)aFunction which you specified is linked in the InterfaceBuilder. When you set in Interface builder you would have set to TouchUpInside. Instead select the Event as ValueChanged.
Change you function name as
-(void)aFunction:(UISegmentedControl*)sender
and link your segment control in the interface builder with the event as ValueChanged. Not as TouchUpInside
Solved. Maybe I do not how to use the Debugger Consolle, but I was stuck because it was telling me that the crash was happening on
self.sgmRassegna.selectedSegmentIndex = 0;
Since the moment that this operation fires a "Value Changed" event, it calls automatically the method apriRassegnePrecedenti. This time it skips the if so it doesn't make the alloc for the View Controller and when it calls:
[vecchieRassegne release];
... boom.
精彩评论