6

背景:我想解僱一個我早先提出的模態視圖,並且馬上提出了我剛剛用新信息解決的相同的viewControlleriOS:消除並顯示ModalViewController而不訪問其父視圖控制器

問題:我沒有這樣做沒有明確指向父ViewController出示的第一ViewController模態非常成功。我正在努力編寫這個類,它不妨礙以前的viewController的代碼。

可能導致:有幾件事情我一直在嘗試:

1)試圖讓父ViewController,在這個時候,我不知道如何訪問。

2)一旦進入母體的不斷積累,我可以簡單地套用下面的代碼:

UIViewController* toPresentViewController = [[UIViewController alloc] init]; 
    [self dismissViewControllerAnimated:YES completion:^{ 
     [parentViewControllerAccessor presentModalViewController:toPresentViewController animated:YES]; 
}]; 

理論上這應該工作給予訪問父viewController。我願意接受其他方式的這樣做。

假設:您沒有權限更改父母ViewController中的任何代碼。

回答

11

你的代碼看起來

例如,當你希望它被解僱火從模式的看法超出這個通知喜歡它應該工作。如果您使用的是iOS 5,則有一個UIViewController屬性,稱爲presentingViewController

@property(nonatomic, readonly) UIViewController *presentingViewController; 

因此,您可以使用此屬性來獲取呈現您的模態控制器的視圖控制器。

注:在iOS 4的parentViewController將被設置爲呈現控制器,因此,如果您同時支持的iOS 4和5,你必須檢查操作系統版本率先哪個屬性決定訪問。在iOS 5中,Apple已經修復了這個問題,因此parentViewController現在專門用於包含的視圖控制器的父級(請參閱UIViewController文檔中實現Container View Controller一節)。

編輯:關於來自塊內訪問self.presentingViewController:由塊被稱爲時間(模態視圖控制器被駁回後)presentingViewController屬性可能會設置爲nil。請記住塊內的self.presentingViewController在塊被執行時給出屬性的值,而不是在創建塊時。爲防止這種情況做到以下幾點:

UIViewController* toPresentViewController = [[UIViewController alloc] init]; 
UIViewController* presentingViewController = self.presentingViewController; 
[self dismissViewControllerAnimated:YES completion:^ 
{ 
    [presentingViewController presentModalViewController:toPresentViewController animated:YES]; 
}]; 

這是必要的,不是因爲self消失/駁回(它是安全地通過塊保留),而是因爲它不再呈現,因此其presentingViewController現在是零。沒有必要在其他地方存儲presentingViewController,局部變量很好,因爲它將被塊保留。

+0

如果我理解正確,我應該改變這段代碼:'[parentViewControllerAccessor presentModalViewController:toPresentViewController animated:YES];'''self.presentingViewController presentModalViewController:croppedPhotoVC animated:YES];'。可悲的是,這似乎並不奏效。我理解錯誤嗎? – Byte

+1

這可能是由於塊如何保留對象 - 它將保留自己而不是呈現視圖控制器,因此在「自我」被解除的時候,該屬性可能被設置爲零。將self.presentingViewController設置爲塊之外的局部變量,然後在該塊內使用該變量。 – jhabbott

+0

這與我提出的結論是一樣的。我可能必須製作一個靜態變量來保存這個,這樣當'self'被移除時,該方法仍然可以工作。但是這看起來更像是一種黑客。 – Byte

1

您可以使用通知完成此操作。

[[NSNotificationCenter defaultCenter] postNotificationName:@"dismissModalView" 
                object:nil 
                userInfo:nil]; 

然後再處理您的模態視圖內部的通知:

- (void)viewDidLoad { 
    [[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(dismissMe:) 
               name:@"dismissModalView" 
               object:nil]; 
} 

- (void)dismissMe:(NSNotification)notification { 
    // dismiss it here. 
} 
+0

「然後處理你的模式視圖內部的通知:」我不知道這是假設的意思是,我沒有明確說過,我無法觸及顯示此viewController的前一個viewController上的代碼。除非,我在這裏誤解了什麼? – Byte

1

對於iOS5的解決方案:

-(void)didDismissModalView:(id)sender { 

    // Dismiss the modal view controller 
    int sold=0; 

    if(sold==0){ 

     //Cash_sold.delegate = self; 
     // Cash_sold.user_amount.text=[NSString stringWithFormat:@"%d",somme]; 

     Cash_sold = [[CashSoldview alloc] initWithNibName:@"CashSoldview" bundle:nil]; 
     CGRect fram1 = CGRectMake(200,20,400,400); 
     Cash_sold.view.superview.frame = fram1; 
     Cash_sold.view.frame=fram1; 
     Cash_sold.modalTransitionStyle= UIModalTransitionStyleCoverVertical; 
     Cash_sold.modalPresentationStyle=UIModalPresentationFormSheet; 

     UIViewController* presentingViewController = self.parentViewController; 

     [self dismissViewControllerAnimated:YES completion:^ 
     { 
     [presentingViewController presentModalViewController:Cash_sold animated:YES]; 
     }];  
    } 
} 
1

試試下面的代碼:

[self dismissViewControllerAnimated:NO 
         completion:^{ 
    // instantiate and initialize the new controller 
    MyViewController *newViewController = [[MyViewController alloc] init]; 
    [[self presentingViewController] presentViewController:newViewController 
               animated:NO 
               completion:nil]; 
}]; 
+0

非常短的代碼,也工作良好.. – svmrajesh

相關問題