iOS使用NavigationController經常出現的問題

來源:互聯網
上載者:User

標籤:ios   navigationcontroller   uinavigationcontroll   push   nil   

情況一: 

MyViewController *sampleViewController = [[[MyViewController alloc]initWithXXX] autorelease];       

[self.navigationController pushViewController: sampleViewController animated:true];

BUG:介面無反應

      分析可能出錯的原因:

1:self.navigationController為nil,null 指標執行pushViewController直接跳過,接著sampleViewController會釋放掉,所以介面會無反應。

   self.navigationController為神馬會空呢?navigationController是UIViewController父類繼承來的屬性,不是隨便都能用的,除非你當前viewController是被nav push進來的,或者用xib/代碼做成navigationcontroller的root了,cocoa的代碼應該是給賦值了。(題外話,我很多介面就是局部用了某個ViewController,這種情況該viewController的nav為空白,如果要正確使用,可以從app取,可以自己賦值,可以整體介面取。。。想咋取都行)

2、self.navigationController為nil,介面掛了

   如果你sampleViewController用到了網路非同步請求,在initWithXXX的時候會去取數,那麼你10有89會掛掉。

   原因分析:大家網路請求,經常如下:

TwitterClient *twitterClient = [[TwitterClient alloc] initWithTarget:self action:@selector(XXXReceive:obj:)];

[twitterClient getXXX:param];

恭喜你,這種情況不管是不是navigationController為空白,你程式極容易掛掉,原因就是因為網路請求我們都會用非同步方式來拉取資料,當資料回來之後,會回調到本類的XXXReceive:obj方法,

但是如果介面退出了,資料才收到將如何?那就是不是null 指標調用了,TwitterClient裡紀錄的需要回調的記憶體位址上神馬情況都有可能是,回調。。杯具了

 

解決辦法:twitterClient做成成員,在析構的時候該咋收拾twitterClient的就咋收拾一下吧


3、self.navigationController為nil,介面掛了

     和上文一樣twitterClient成員了,dealloc也清理了現場,還是會掛

原因分析:非同步問題,twitterClient已經取到資料,但是sampleViewController已經被釋放了,but dealloc根本沒來得及進來。。。

    解決辦法,1:確保nav正確,2:盡量在介面viewDidLoad的時候網路請求資料,在dispear的時候清理網路。


情況二:

reason: ‘Pushing a navigation controller is not supported‘

報上面的錯誤,原因是:

大家知道,可以將ViewController push到一個 NavigationController中。就像是入棧操作!

將一個NavigationController再次push到NavigationController中時,報錯,出現了

reason: ‘Pushing a navigation controller is not supported‘的錯誤。

應該是,NavigationController不支援push進來的對象是NavigationController吧。


修改方法是:將要進入的頁面不去繼承UINavigationController


情況三:

有時候項目裡會有一種訊息推送, 就是彈出提示使用者訊息, 而訊息中帶有連結, 當你點擊串連時不能跳轉,

這種情況是因為你的self.navigationController = nil 造成的,

因為你彈窗的那個控制器沒有用UINavigationController封裝



iOS使用NavigationController經常出現的問題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.