簡體   English   中英

UIbutton崩潰的應用程序,除了(lldb)以外沒有其他錯誤

[英]UIbutton crashing app with no error other than (lldb)

我正在研究視圖A(createExerciseViewController),該視圖在單擊UIButton之后添加了視圖B(createRoutinePopupViewController)。

這部分工作正常,並且添加了視圖。

然后在視圖B內部(createRoutinePopupViewController),我有另一個UIButton。 當我單擊此UIButton時,應用程序崩潰,我得到的所有錯誤均為(lldb)並且未執行NSLog。

但是然后有時並且只有有時幾次崩潰之后,一切都會正常執行...

我對iOS開發人員世界還很陌生,我不知道自己可能做錯了什么。

所有的UIButton方法都很strong

有誰知道為什么會這樣?

我認為問題可能出在我如何插入子視圖和處理整個子視圖中?

一個---- createExerciseViewController.m

#import "createExerciseViewController.h"
#import "createExercisePopupViewController.h"
#import "createRoutinePopupViewController.h"

// ....more code

- (IBAction)createRoutine:(id)sender {
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Storyboard" bundle:nil];
    [self.view addSubview:[[storyboard instantiateViewControllerWithIdentifier:@"createRoutinePopupView"] view]];
}

這是UIViewController B ---- createRoutinePopupViewController.m

#import "createRoutinePopupViewController.h"
#import "createExerciseViewController.h"
#import "User.h"
#import "Routine.h"

- (IBAction)createRoutine:(UIButton *)sender {
    NSLog(@"Please dont crash");
  }

在此處輸入圖片說明

您不應該只是為了將視圖控制器添加到其他視圖控制器的視圖而隨意創建視圖控制器。 您需要告訴系統您正在將視圖從一個控制器移動到另一個控制器,以便它可以做家務。 如果您不這樣做,那么一個視圖控制器最終將擁有另一個視圖控制器正在呈現的視圖,因此事件和觸摸等會變得混亂。 可能是導致崩潰的原因。

iOS現在提供了一種“容器視圖控制器”機制來管理這種情況,即您告訴系統您正在將視圖從一個控制器移動到另一個控制器。

蘋果的文檔

實現容器的目標是能夠將另一個視圖控制器的視圖(和關聯的視圖層次結構)添加為容器的視圖層次結構中的子樹。 子級仍然負責其自己的視圖層次結構,除了容器決定將其放置在屏幕上的位置之外。 添加子視圖時,需要確保事件繼續分配給兩個視圖控制器。 您可以通過將新的視圖控制器顯式關聯為容器的子級來實現。

實際上,它比聽起來簡單。 在createExerciseViewController.m中嘗試以下操作:

    - (IBAction)createRoutine:(id)sender {
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Storyboard" bundle:nil];

        CreateRoutinePopupViewController* popupController = [storyboard instantiateViewControllerWithIdentifier:@"createRoutinePopupView"];

        //Tell the operating system the CreateRoutine view controller
        //is becoming a child:
        [self addChildViewController:popupController];

        //add the target frame to self's view:
        [self.view addSubview:popupController.view];

        //Tell the operating system the view controller has moved:
        [popupController didMoveToParentViewController:self];
    }

嘗試這個:

    CreateRoutinePopupViewController *popUp = [[storyboard instantiateViewControllerWithIdentifier:@"createRoutinePopupView"];
   [self presentModalViewController:popUp Animated:YES];

暫無
暫無

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

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