2012-03-20 51 views
0

我想在我的iPhone應用程序中執行IBAction後顯示特定的ViewController(或關閉)視圖。我試過執行操作後顯示或解除特定的ViewController

[self.parentViewController.parentViewController dismissModalViewControllerAnimated:YES]; 

但是,一旦行動已經執行,這似乎沒有做任何事情。

多一點信息:

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

{ 
    UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath]; 
    if (selectedCell.tag == 1) 
    { 

     UIActionSheet *actionSheet = [[UIActionSheet alloc] 
             initWithTitle:@"Are you sure you want to delete this project?" 
     delegate:self cancelButtonTitle:@"No" destructiveButtonTitle:@"Yes, I’m Sure" otherButtonTitles:nil]; 
     [actionSheet showInView:self.view]; 


    } 

} 

- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex 
{ 
    if (buttonIndex != [actionSheet cancelButtonIndex]) 
    { 

     [self.tableView beginUpdates]; // Avoid NSInternalInconsistencyException 

     // Delete the project object that was swiped 
     Project *projectToDelete = self.project; 
     NSLog(@"Deleting (%@)", projectToDelete.name); 
     [self.managedObjectContext deleteObject:projectToDelete]; 
     [self.managedObjectContext save:nil]; 

    } 
} 

我想,當用戶按下actionsheet是按鈕當前的視野中消失。

+0

假設自我指的是當前顯示的,你爲什麼不使用[自dismissModal ...]的UIViewController中。根據文檔,該呼叫被自動轉發到呈現視圖控制器。然而,呈現視圖控制器通常是self.parentViewController而不是self.parentViewController.parentViewController,除非你有一些不尋常的視圖層次結構。 – 2012-03-20 13:11:10

回答

1

我需要回到我的導航堆棧中的第一個(或根)視圖。

所有我需要做的就是用這個方法:

[controller.navigationController popToRootViewControllerAnimated:YES]; 
1
// Assume we are inside a UIViewController (or a subclass) 

DestinationController *destinationController = [[DestinationController alloc] init]; 
[self presentModalViewController:destinationController animated:YES]; 

... 

// Assume we are now in destination controller 

// Dismiss 
[self dismissModalViewControllerAnimated:YES]; 
+0

我得到一個'NSInvalidArgumentException',原因:'使用這個 – jcrowson 2012-03-20 13:35:30

+0

@LordSnoutimus時,NSFetchedResultsController的一個實例需要一個非零fetchRequest和managedObjectContext錯誤好吧,這是一個不同的故事。這意味着您正在使用'NSFetchedResultsController'來使用coredata獲取託管對象(顯然是作爲UITableViewController的數據源),但是您沒有提供獲取請求和上下文。 [看更多](http://developer.apple.com/library/ios/#documentation/CoreData/Reference/NSFetchedResultsController_Class/Reference/Reference.html) – Alladinian 2012-03-20 13:45:35

0

顯示和解除視圖控制器的另一種方法是使用pushViewController和popViewController。

要顯示的viewController:

- (無效)pushViewController:(的UIViewController *)的viewController動畫:(BOOL)動畫; //使用水平幻燈片切換。如果視圖控制器已經在堆棧中,則不起作用。

並關閉:

- (UIViewController的*)popViewControllerAnimated:(BOOL)動畫; //返回彈出的控制器。

+0

雖然這是正確的,但這些方法只能在使用'UINavigationController' – Alladinian 2012-03-20 13:26:15

相關問題