ios - UIView 没有出现

标签 ios cocoa-touch uiview

所以我有这段代码-

-(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/

相关文章:

iphone - 强制加载/卸载 View

iOS >> 拖放问题 >> UIView 返回原始位置

ios - ComponentKit 与 AsyncdisplayKit

iphone - 使用正则表达式验证文本字段

ios - UINavigationController 的自定义 UIBarButtonItem/UISegmentedControl

ios - 缩放字体大小以垂直适合 UILabel

ios - 多个UIView的重叠

ios - 将 UIView 或 UIViewController 用于计时器对象?

ios - UICollectionView 图像被拉伸(stretch)

ios - Rubymotion 图像按钮事件未触发