无法隐藏 UINavigationBar 或在 TabBarController 中加载内部 HTML

Posted

技术标签:

【中文标题】无法隐藏 UINavigationBar 或在 TabBarController 中加载内部 HTML【英文标题】:Can't hide UINavigationBar or load internal HTML in TabBarController 【发布时间】:2011-08-14 09:15:02 【问题描述】:

我在标签栏控制器中隐藏导航栏时遇到问题。初始选项卡正确隐藏,但任何后续选项卡都不会隐藏其导航栏。我遇到的另一个问题是同一个 html 文件不会加载到其他两个选项卡中。这真的很奇怪,因为我已经让 html 在其他应用程序中加载得很好。

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:    (NSDictionary *)launchOptions
// Override point for customization after application launch.

        //create views
    testView *viewer1 = [[testView alloc] init];
    testView2 *viewer2 = [[testView2 alloc] init];
    testView3 *viewer3 = [[testView3 alloc] init];

    // Put viewers in a Nav controllers 
    UINavigationController *navCon1 = [[UINavigationController alloc] init];
    [navCon1 setViewControllers:[NSArray arrayWithObject:viewer1] animated:NO];
    UINavigationController *navCon2 = [[UINavigationController alloc] init];
    [navCon1 setViewControllers:[NSArray arrayWithObject:viewer2] animated:NO];
    UINavigationController *navCon3 = [[UINavigationController alloc] init];
    [navCon1 setViewControllers:[NSArray arrayWithObject:viewer3] animated:NO];

    // Put controllers in a tab bar
    UITabBarController *tabBar = [[UITabBarController alloc] init];
    tabBar.viewControllers = [NSArray arrayWithObjects:navCon1,navCon2,navCon3, nil];

    //Release all the stuff
    [viewer1 release]; [viewer2 release]; [viewer3 release];
    //[navCon1 release]; [navCon2 release]; [navCon3 release];

    [self.window addSubview:tabBar.view];

    [self.window makeKeyAndVisible];
    return YES;


// Test views are just a UIWebView as a sub view built in IB and then connected to the //IBOutlet and connected to be the delegate. 

//Here is TestView.m

    @implementation testView

    @synthesize webView;

    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
         if (self) 
        // Custom initialization
        self.navigationController.navigationBarHidden = YES;
    
        return self;
    

    - (void)didReceiveMemoryWarning
    
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
    

    #pragma mark - View lifecycle

    - (void)viewDidLoad
    
        [super viewDidLoad];

        // Do any additional setup after loading the view from its nib.
        NSString *path = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"           inDirectory:@"HTMLsupport"];
        NSURL *url = [NSURL fileURLWithPath:path];
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [webView loadRequest:request];
    

    - (void)viewDidUnload
    
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    self.webView = nil;
    

    - (BOOL)shouldAutorotateToInterfaceOrientation:  (UIInterfaceOrientation)interfaceOrientation
    
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
    

    @end

我还尝试使用这个新的 .m 在每个委托函数中设置属性 在我粘贴之前,这是打印出来的日志

2011-08-14 04:23:23.857 startRipple2[1613:b303] viewDidLoad
[Switching to process 1613 thread 0xd603]
2011-08-14 04:23:23.859 startRipple2[1613:b303] viewWillAppear
2011-08-14 04:23:23.860 startRipple2[1613:b303] viewDidAppear
2011-08-14 04:23:23.860 startRipple2[1613:b303] its hidden
[Switching to process 1613 thread 0x12103]
2011-08-14 04:23:27.270 startRipple2[1613:b303] viewWillAppear
2011-08-14 04:23:27.271 startRipple2[1613:b303] viewDidAppear
2011-08-14 04:23:27.273 startRipple2[1613:b303] its hidden

但它仍然存在!值得注意的是,单击第二个选项卡后不会调用 viewDidLoad。

    @synthesize webView;

    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) // Custom initialization

        [self.navigationController setNavigationBarHidden:YES animated:YES]; 
    
    return self;
    


    - (void)didReceiveMemoryWarning
    
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
    

    #pragma mark - View lifecycle



    - (void)viewDidLoad
    
    [super viewDidLoad];
    self.navigationController.navigationBarHidden = YES;
    NSString *path = [[NSBundle mainBundle] pathForResource:@"about" ofType:@"html" inDirectory:@"tabA"];
    NSURL *url = [NSURL fileURLWithPath:path];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [webView loadRequest:request];
    webView.scalesPageToFit = YES;
    // Do any additional setup after loading the view from its nib.
    NSLog(@"viewDidLoad");
    
    - (void)viewWillAppear:(BOOL)animated
    self.navigationController.navigationBarHidden = YES;
    NSLog(@"viewWillAppear");
    

    - (void)viewDidAppear:(BOOL)animated
     //self.navigationController.navigationBarHidden = YES;
    NSLog(@"viewDidAppear");
    if (self.navigationController.navigationBarHidden) 
        NSLog(@"its hidden");
    

    if (!self.navigationController.navigationBarHidden)
        NSLog(@"its there...");
    
    

    - (IBAction)mySelector:(id)sender 
    NSLog(@"You touched me THERE!");
    [self.webView goBack];
    [self.navigationController setNavigationBarHidden:YES];
    

    -(void)awakeFromNib
    NSLog(@"awakeFromNib");
    [self.navigationController setNavigationBarHidden:YES];
    

    - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request  navigationType:(UIWebViewNavigationType)navigationType
    //int count =0;
    //count = count +1 ;
    //NSLog([NSString stringWithFormat:@"count %d",count]);

    return YES;
    



    - (void)webViewDidFinishLoad:(UIWebView *)webView
    /*if (navigationType == UIWebViewNavigationTypeLinkClicked || navigationType == UIWebViewNavigationTypeFormSubmitted) 
        [self.navigationController setNavigationBarHidden:NO];
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(mySelector:)];*/


    - (void)viewDidUnload
    
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    self.webView = nil;
    

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
    

    @end

【问题讨论】:

【参考方案1】:

首先,您的代码中有一个错误。在您的 applicationDidFinishLaunching 中,您始终将视图控制器设置为 navcon1 而不是 navcon1,然后是 navcon2,然后是 navcon3。

修复此问题并检查问题是否仍然存在

【讨论】:

So '// 将查看器放入导航控制器 UINavigationController *navCon1 = [[UINavigationController alloc] init]; [navCon1 setViewControllers:[NSArray arrayWithObject:viewer1] 动画:NO]; UINavigationController *navCon2 = [[UINavigationController alloc] init]; [navCon2 setViewControllers:[NSArray arrayWithObject:viewer2] 动画:NO]; UINavigationController *navCon3 = [[UINavigationController alloc] init]; [navCon3 setViewControllers:[NSArray arrayWithObject:viewer3] 动画:NO];' 我只是想拥有三个独立的导航控制器,每个标签一个。然后这些导航控制器每个只拥有一个视图。你说的是这个吗? 是的,试试我发布的内容。您犯了一个错误,因为您从未将 rootviewcontroller 分配给 navCon2 和 navCon3,而是覆盖了来自 navcon1 的那些。我猜是复制和粘贴错误:) 哇...很棒的发现!猜猜这就是我早上 5 点编码的结果……下次我复制和粘贴一行时必须记住这个错误。

以上是关于无法隐藏 UINavigationBar 或在 TabBarController 中加载内部 HTML的主要内容,如果未能解决你的问题,请参考以下文章

错误的窗口令牌,您无法在创建 Activity 之前或在广播接收器中隐藏异常之后显示对话框

无法隐藏子图轴标签或在matplotlib中设置MaxNLocator

UINavigationBar 隐藏在 statusBar [重复]

如何隐藏 UINavigationBar 和 UITabBar 动画?

如何从 UINavigationController 隐藏 UINavigationBar?

如何隐藏 UINavigationBar