所以我有这段代码 -
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
self.searchController = [[PlacesViewController alloc]initWithNibName:@"PlacesView" bundle:nil];
NSLog(@"TEXT FIELD DID BEGIN EDITIN");
self.navigationItem.hidesBackButton = YES;
[self.navigationController pushViewController:self.searchController animated:YES];
return NO;
}
NSLog
会运行,但我的视图不会显示。这发生在控制器中,该控制器具有导航栏中存在的视图。像这样 -
-(void)addSearchBar{
self.searchBarController = [[SearchBarController alloc] initWithNibName: @"SearchBar" bundle: nil];
self.navigationItem.titleView = self.searchBarController.view;
}
在initWithNibName
方法中调用addSearchBar。
为什么我的观点没有出现?
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
searchQuery = [[SPGooglePlacesAutocompleteQuery alloc] init];
searchDetailQuery = [[SPGooglePlacesPlaceDetailQuery alloc] init];
searchQuery.radius = 2000;
shouldBeginEditing = YES;
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
}
return self;
}
我也证明了这个观点是有效的,因为当我这样做时 -
-(void)textFieldDidBeginEditing:(UITextField *)textField{
self.searchController = [[PlacesViewController alloc]initWithNibName:@"PlacesView" bundle:nil];
//may have to be strong.
NSLog(@"SELF VIEW : %@", self.view);
textField.inputView.center = self.view.center;
textField.inputView.bounds = self.view.bounds;
textField.inputView = self.searchController.view;
}
视图显示在键盘的位置。
答案 0 :(得分:1)
您可以将addSearchBar从initWithNibName
移至viewDidLoad
进行试用。实际上,通常在init
方法中,视图不会初始化。该视图在首次访问时将被懒惰地初始化,通常是在屏幕上显示视图时,并且在初始化视图后将立即调用viewDidLoad
方法。因此,init
方法通常只配置与数据相关的属性,而视图相关属性通常在viewDidLoad
方法中配置。