我有一个视图控制器,在导航项中有一个UITextField。此视图控制器也是uitextfield的委托,因此它实现了-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
。
但是这个方法里面有一点问题。当我想为这个类使用任何全局属性时,它们都是零,我有这样的:
- (id) initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if(self){
FirstViewController *lvc = [[FirstViewController alloc] initWithNibName:@"FirstView" bundle:nil];
SecondViewController *mvc = [[SecondViewController alloc] initWithNibName:@"SecondView" bundle:nil];
ThirdViewController *lsc = [[ThirdViewController alloc] initWithNibName:@"ThirdView" bundle:nil];
FourthViewController *fvc = [[FourthViewController alloc] initWithNibName:@"FourthView" bundle:nil];
FifthViewController *svc = [[FifthViewController alloc] initWithNibName:@"FifthView" bundle:nil];
viewControllers = [[NSArray alloc] initWithObjects:lvc, mvc, lsc, fvc, svc, nil];
lastIndex = 0;
isLogged = NO;
[lvc release]; [mvc release]; [lsc release]; [fvc release]; [svc release];
}
return self;
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
SearchView *searchView = [[SearchView alloc] initWithNibName:@"SearchView" bundle:nil andTextField:textField];
// I need to set up delegate here
// [searchView setDelegate:[viewControllers objectAtIndex:0]];
// but viewControllers is nil in here
AppDelegate *delegate = [[UIApplication sharedApplication] delegate];
[delegate.navigationController pushViewController:searchView animated:YES];
delegate = nil;
[searchView release];
return NO;
}
正如在代码中所解释的,viewControllers数组在textFieldShouldBeginEditing方法中是nil,所以我无法在我需要的地方设置委托...为什么属性在哪里?我能解决吗?
感谢。