簡體   English   中英

ViewController只會加載一次,當我返回時應用崩潰,然后再次加載

[英]ViewController will only load once, app crashes when I go back then load it again

這里的目標C新手。 我從UITableViewCell鏈接到ViewController子類。 在我的故事板上,我有一個包含UITableView的UIViewController以及一個帶有標識符“ Subject”的自定義類SubjectViewController。

我還將獲取所選單元格中包含的文本,將其添加到NSUserDefaults中,並將其用作“主題”頁面的標題。

當我啟動應用程序時,第一次單擊UITableViewCell時,SubjectViewController會完美啟動。 我在該頁面上具有的任何功能都可以正常工作。 但是,當我返回主屏幕,然后單擊另一個UITableViewCell時,該應用程序凍結,並且我僅收到消息“ 11db”。

這是涉及的兩種方法:

從ViewController.m

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    // Store cell label in NSUserDefaults to later display as the title
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    NSString *cellID = cell.textLabel.text;

    [defaults setObject:cellID forKey:@"sceneTitle"];
    [defaults synchronize];

    //Present subjects view controller
    NSString * storyboardName = @"Main";
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];

    SubjectViewController * subject = [storyboard instantiateViewControllerWithIdentifier:@"Subject"];
    [self presentViewController:subject animated:YES completion:nil];
}

從SubjectViewController.m

- (IBAction)subjectBackPressed:(id)sender {
    NSString *storyboardName = @"Main";
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];

    NSString *home = @"Head and Neck";
    SubjectViewController * vc = [storyboard instantiateViewControllerWithIdentifier: home];
    [self presentViewController:vc animated:YES completion:nil];
}

讓我知道更多的代碼/信息是否有幫助,我將編輯這篇文章。

我建議要么用self.dismissViewController消除當前視圖,要么執行一次返回UIViewController的任務

如果從主故事板上加載了vc1,則無需再次初始化。 只需訪問self.storyboard將提供故事板的已分配實例。

如果要顯示vc2,則應通過關閉它來將其刪除。 后退動作應該是

[self dismissViewControllerAnimated:YES完成:無];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM