所以我有这段代码-
-(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
运行,但我的 View 没有出现。这发生在具有导航栏中存在的 View 的 Controller 中。像这样 -
-(void)addSearchBar{
self.searchBarController = [[SearchBarController alloc] initWithNibName: @"SearchBar" bundle: nil];
self.navigationItem.titleView = self.searchBarController.view;
}
在 initWithNibName
方法中调用了 addSearchBar。
为什么我的 View 没有出现?
- (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;
}
我还证明了该 View 有效,因为当我这样做时 -
-(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;
}
View 出现在键盘的位置。
最佳答案
您可以将 addSearchBar 从 initWithNibName
移动到 viewDidLoad
进行尝试。事实上,通常在 init
方法中, View 不会初始化。第一次访问 View 时,通常是在屏幕上显示 View 时, View 将被延迟初始化,并且 View 初始化后将立即调用 viewDidLoad
方法。所以通常init
方法只配置数据相关的属性,而 View 相关的属性通常在viewDidLoad
方法中配置。
关于ios - UIView 没有出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20837534/