2012-09-29 18 views
1

我有其上呈現這樣的視圖控制器稱爲VC0:dismissViewController:不工作

[self presentViewController: vc1 animated: YES completion: nil]; 

而在VC1我有一個按鈕來呈現另一視圖控制器:在VC2

[self presentViewController: vc2 animated: YES completion: nil]; 

然後,我有一個按鈕關閉視圖控制器:

[self dismissViewControllerAnimated:YES completion: ^{ 
// over here I call one method in vc1 
} 

和預期的一樣它返回到VC1 ..但有在VC1一個按鈕,通過駁回這樣的視圖控制器回到VC0:

[self dismissViewControllerAnimated:YES completion:nil]; 

但由於某種原因,這是行不通的,視圖控制器不會被開除回VC0。當我第一次出現vc1時,我可以按下按鈕來關閉視圖控制器並且它可以工作。但是當我按下按鈕打開vc2,並且當我將vc2退回到vc1時,然後我按下按鈕關閉視圖控制器,即當它不起作用時。

對不起,如果這個問題有點不清楚,那麼說出我想說的話有點難。

另外一兩件事:

我試圖在VC1手動目前VC0更換dismissViewControllerAnimated:,但後來我得到一個登錄控制檯說,我想提出一個VC0但VC1的觀點是不是在窗口層次結構。這是什麼意思?

感謝您的幫助!

UPDATE:

在本案例VC0 IS MenuMileIndexViewController - VC1 IS FlightViewController - VC2 IS BookmarksTableViewController

這裏涉及代碼:

MenuMileIndexViewController:

- (IBAction)goToOriginPage { 

FlightRecorder *origin = [[FlightRecorder alloc] init]; 
[self presentViewController:origin animated:YES completion:nil]; 

} 

飛行記錄:

- (void)searchBarBookmarkButtonClicked:(UISearchBar *)searchBar { 

     [self bringUpBookmarkkTable]; 
} 

- (void) bringUpBookmarkkTable { 

    BookmarkTableViewController *bookmarkTVC = [[BookmarkTableViewController alloc] init]; 

    [bookmarkTVC setModalTransitionStyle: UIModalTransitionStyleFlipHorizontal]; 

    [self presentViewController:bookmarkTVC animated:YES completion:nil]; 
} 

- (IBAction)cancel { 

[self dismissViewControllerAnimated:YES completion:nil]; 

} 

- (void)endBookmarkProcessWithBookmarkCollection: (NSDictionary *)dict { 

    presetBookmarkContext = [dict mutableCopy]; 

    bookmarkMode = YES; 

    NSString *compiledText = nil; 

    NSNumber *number1 = [NSNumber numberWithInt: 1]; 

    if ([dict objectForKey: @"bookmarkTag"] == number1) { 

     compiledText = [NSString stringWithFormat: @"%@ to %@", [dict objectForKey: @"origin"], [dict objectForKey: @"destination"]]; 
    } 
    else { 

     compiledText = [NSString stringWithFormat: @"%@ to %@", [dict objectForKey: @"destination"], [dict objectForKey: @"origin"]]; 
    } 

    compiledText = [compiledText stringByReplacingOccurrencesOfString:@"Origin: " withString:@""]; 

    compiledText = [compiledText stringByReplacingOccurrencesOfString:@"Destination: " withString:@""]; 

    flightContext = [NSDictionary dictionaryWithObjectsAndKeys: [dict objectForKey: @"miles"], @"miles", compiledText, @"location", [[NSUserDefaults standardUserDefaults] objectForKey: @"tempD"], @"date", nil]; 

    NSString *string = [NSString stringWithFormat: @"\nMiles: %.2f\nFlight: %@\nDate: %@", [[dict objectForKey: @"miles"] floatValue], compiledText, [[NSUserDefaults standardUserDefaults] objectForKey:@"tempD"]]; 

    UIAlertView *bvkBookmarkAlertView = [[UIAlertView alloc] initWithTitle:@"Confirmation" message:string delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Add", nil]; 

    [bvkBookmarkAlertView show]; 
} 



- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex { 

    if (buttonIndex == 1) { 

     [self cancel]; // Even though cancel is an IBAction, IBAction is the same thing as void so it is callable 
    } 
} 

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { 

    if (buttonIndex == 1) { 

     [TheMileIndexViewController addDesiredMilesToIndex: [[flightContext objectForKey: @"miles"] doubleValue]]; 

     [TravelLogViewController addFlight: flightContext]; 

     if (!bookmarkMode) { 

      if ([checkbox isSelected]) { 

       [BookmarkHandler uploadBookmark: bookmarkFlightContext]; 
      }  
     } 
    } 

    if (buttonIndex == 0) { 

     if ([alertView.title isEqualToString: @"Confirmation"]) { 

      bookmarkMode = NO; 
     } 
    } 

} 

BookmarksTableViewController:

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

    [tableView deselectRowAtIndexPath:indexPath animated: YES]; 

    NSDictionary *dict = [[BookmarkHandler bookmarkCollection] objectAtIndex: indexPath.row]; 

    fl = [[FlightRecorder alloc] init]; 

    [self dismissViewControllerAnimated:YES completion:^{ 

     [fl endBookmarkProcessWithBookmarkCollection: dict]; 
    }]; 
} 

現在,我已經創造了模擬器的應用中展示什麼問題的屏幕錄製。我可以通過電子郵件發送給您以供參考。所以我可以發郵件給你。

+0

寫控制檯消息 – Baig

+0

如何以及在何處您重新設定按鈕的操作? – geraldWilliam

+0

在xib文件中 – MCKapur

回答

3

我會建議總是從風險投資中解散一個風險投資,它使用委託。這實際上也是蘋果推薦的方式 - 就像我之前對這個問題的回答所指出的那樣。

所以如果你有VC0提供VC1,也有VC0退出VC1代碼,使用委託方案。

我已經瞭解到,這是處理提交和解僱的最佳途徑 - 儘管有時候它會在VC1內部解僱VC1。

我問過一個非常相關的問題,您可能也有興趣檢查一下。它顯示code...

ps我也讀了一些解僱VC1 - 這反過來也將解僱VC2。但是,如果我以前的建議有效,我不會這樣做。有時候,我執行(不崩潰)的VC已經不存在或任何與該過程中獲得的信息 - 所以我認爲這不是最好的解決方案。但如果我的先前建議不起作用,你可以嘗試第二個。

雖然不能保證,這將持續更新,新的iOS,因爲這個問題一直hauting我好iOS的更新現在:-),所以我決定去推薦標準路線。

編輯: 這是我修改後的代碼 - 它的工作原理沒有問題 - 但是不清楚你打算在用戶迴應Alert後發生什麼,以及警報是否應該在VC1或VC0上。無論如何,使用委託和回調我沒有看到任何問題。請解釋我已經錯過了你的觀點......

FlightViewControllerProtocol.h

@protocol FlightViewControllerProtocol <NSObject> 
-(void) dismissVCAndEndBookmark; 
@end 

FlightViewController.m

#import "FlightViewController.h" 
#import "FlightViewControllerProtocol.h" 
#import "BookmarksTableViewController.h" 
@interface FlightViewController() 

@end 

@implementation FlightViewController 
@synthesize delegate; 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view from its nib. 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

- (void)searchBarBookmarkButtonClicked:(UISearchBar *)searchBar { 

    [self bringUpBookmarkkTable]; 
} 

- (IBAction) bringUpBookmarkkTable { 

    BookmarksTableViewController *bookmarkTVC = [[BookmarksTableViewController alloc] init]; 
    bookmarkTVC.delegate = self; 
    [bookmarkTVC setModalTransitionStyle: UIModalTransitionStyleFlipHorizontal]; 

    [self presentViewController:bookmarkTVC animated:YES completion:nil]; 
} 

- (IBAction)cancel { 

    [self dismissViewControllerAnimated:YES completion:nil]; 

} 

- (void)endBookmarkProcessWithBookmarkCollection: (NSDictionary *)dict { 

// presetBookmarkContext = [dict mutableCopy]; 

// bookmarkMode = YES; 

    NSString *compiledText = nil; 

    NSNumber *number1 = [NSNumber numberWithInt: 1]; 

    if ([dict objectForKey: @"bookmarkTag"] == number1) { 

     compiledText = @"Text1"; 
    } 
    else { 
     compiledText = @"Text2"; 
    } 


// flightContext = [NSDictionary dictionaryWithObjectsAndKeys: [dict objectForKey: @"miles"], @"miles", compiledText, @"location", [[NSUserDefaults standardUserDefaults] objectForKey: @"tempD"], @"date", nil]; 

    NSString *string = compiledText; 

    UIAlertView *bvkBookmarkAlertView = [[UIAlertView alloc] initWithTitle:@"Confirmation" message:string delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Add", nil]; 

    [bvkBookmarkAlertView show]; 
} 


- (void) dismissVCAndEndBookmark { 
    [self dismissViewControllerAnimated:YES completion:nil]; 
    [self endBookmarkProcessWithBookmarkCollection: nil]; 
} 

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex { 

    if (buttonIndex == 1) { 

     [self cancel]; // Even though cancel is an IBAction, IBAction is the same thing as void so it is callable 
    } 
} 

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { 

    if (buttonIndex == 1) { 

     NSLog(@"alertView1"); 
      } 

    if (buttonIndex == 0) { 
     NSLog(@"alertView2"); 

    } 

} 

@end 

BookmarksTableViewController.h

@interface BookmarksTableViewController : UIViewController 
{ 
    id delegate; 
} 

@property (nonatomic,strong) id delegate; 

@end 

BookmarksTableViewController.m

- (IBAction)goBack { 
    [self.delegate dismissVCAndEndBookmark]; 
} 

特別是,如果我正確理解你的意圖,在BookmarksTableViewController.m中的回調似乎是你的實現中的主要問題。

+0

對這個問題的回答,以及你引用「這種做法的好方法」是我正在做的......但爲什麼它不工作 – MCKapur

+0

你正在做:[self dismissViewControllerAnimated:YES completion:^ { //在這裏,我調用VC2中的一個方法,在VC2中不會像[委託解散VC],這將在VC1中執行。另外嘗試動畫:否 - 但請確保您從VC1內部解除了VC2並從VC0內部解除了VC1 – user387184

+0

爲什麼我應該使用委託?蘋果已經爲我提供了一個完成塊....我會嘗試動畫NO – MCKapur

0

第一次查看:

首先嵌入導航控制器的第一視圖。 並使用此代碼導航到另一個視圖

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle: nil]; 
FirstView *rvc = [storyboard instantiateViewControllerWithIdentifier:@"apps"]; 
[self.navigationController pushViewController:rvc animated:YES]; 

第二種觀點:

的方法添加這一行辭退查看

- (IBAction)back{ 
    [self.navigationController popViewControllerAnimated:YES]; 
} 
+0

爲什麼我需要創建一個導航控制器? – MCKapur

2
[self.navigationController popViewControllerAnimated:YES]; 

的伎倆我也。在我的情況下,我有一個(iPhone)viewController使用push segue推入堆棧。因爲這是啓動的賽格瑞的viewController有一個導航欄,我不得不送父控制器的navigationController的popViewControllerAnimated消息,而不是調用它自己的dismissViewControllerAnimated的:完成消息。

+0

我一直認爲popViewController:如果您使用過pushViewController,那麼它會返回:在這種情況下,您必須有一個導航控制器來完成所有這些 - 我不會 – MCKapur

+1

是的。抱歉,我錯過了原始帖子的具體細節。我試圖指出,在segues中,你可能會在不知不覺中在NavigationController「下」,所以彈出可能是方法而不是解僱。 –