菜鸟教程小白 发表于 2022-12-11 22:07:58

objective-c - 从 Pushed UIViewController 访问 Root UINavigationController


                                            <p><p>我的项目遇到了问题。</p>

<p>每次我尝试从推送的 UIViewController 访问我的 UINavigationController 时,我都会得到“(null)”作为返回。</p>

<p>所以这是我的项目结构:</p>

<pre><code>1. AppDelegate.m initalises UINavigation Controller
2. UINavigation Controller starts with Welcome Page
3. Welcome Page Pushes New UIViewController on Button Press
4. UIViewController returns (null) when self.navigationController called
</code></pre>

<p>好的,这里有一些代码,在我的欢迎页面中我称之为:</p>

<pre><code>MyClass *theChatRoom = [ initWithNibName:@&#34;ChatRoom&#34; bundle:nil];
];
;
</code></pre>

<p>效果很好,它插入了新的 ViewController 。</p>

<p>然后在 MyClass.m 中:</p>

<pre><code>@implementation MyClass

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = ;
    if (self) {
      NSLog();
    }
    return self;
}
</code></pre>

<p>这会返回 (null)</p>

<p>我做错了什么? </p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>根据文档,<code>UIViewController</code>的<code>navigationController</code>:</p>

<blockquote>
<p>This property is nil if the view controller is not embedded inside a
navigation controller.</p>
</blockquote>

<p>也就是说,如果您在嵌入到 <code>UINavigationController</code> 之前访问 <code>navigationController</code>(尚未推送),它将返回 nil。因此,您可以在 <code>viewDidLoad</code> 中访问它,但在 <code>- (id)initWithNibName:bundle:</code> 中不能访问它。</p></p>
                                   
                                                <p style="font-size: 20px;">关于objective-c - 从 Pushed UIViewController 访问 Root UINavigationController,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/9732399/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/9732399/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: objective-c - 从 Pushed UIViewController 访问 Root UINavigationController