2012-09-29 21 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“下”,所以弹出可能是方法而不是解雇。 –