dismissViewController:不起作用

MCK*_*pur 5 iphone xcode objective-c viewcontroller ios

我有一个名为vc0的视图控制器,它的显示方式如下:

[self presentViewController: vc1 animated: YES completion: nil];
Run Code Online (Sandbox Code Playgroud)

在vc1中,我有一个按钮来呈现另一个视图控制器:

[self presentViewController: vc2 animated: YES completion: nil];
Run Code Online (Sandbox Code Playgroud)

然后在vc2中,我有一个按钮来关闭视图控制器:

[self dismissViewControllerAnimated:YES completion: ^{
// over here I call one method in vc1
}
Run Code Online (Sandbox Code Playgroud)

并按预期方式将其返回到vc1。.但是,在vc1中有一个按钮可通过关闭视图控制器来返回到vc0,如下所示:

    [self dismissViewControllerAnimated:YES completion:nil];
Run Code Online (Sandbox Code Playgroud)

但是由于某种原因它无法正常工作,因此视图控制器不会退回到vc0。当我第一次展示vc1时,我可以按下按钮以关闭视图控制器,并且它可以正常工作。但是,当我按下按钮打开vc2,并且当我将vc2退回到vc1,然后我按下按钮以关闭视图控制器时,那是不起作用的。

很抱歉,如果这个问题还不清楚,我很难说出我想说的话。

还有一件事:

我尝试替换dismissViewControllerAnimated:vc1以手动显示vc0,但随后在控制台中收到一条日志,提示我正在尝试显示vc0,但vc1的视图不在窗口层次结构中。这是什么意思?

感谢帮助!

更新:

在这种情况下,VC0是MenuMileIndexViewController-VC1是FlightViewController-VC2是BookmarksTableViewController

这里涉及的代码:

MenuMileIndexViewController:

- (IBAction)goToOriginPage {

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

}
Run Code Online (Sandbox Code Playgroud)

飞行记录仪:

    - (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;
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

书签TableViewController:

    - (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];
    }];
}
Run Code Online (Sandbox Code Playgroud)

现在,我已经在模拟器中创建了该应用程序的屏幕录像,以显示问题所在。我可以通过电子邮件发送给您以供参考。所以我可以通过电子邮件发送给您。

use*_*184 5

我建议始终使用委托将VC与实际提供它的VC分开。实际上,这也是Apple推荐的方式-正如我之前对这个问题的回答所指出的那样。

因此,如果您有VC0提供VC1,也可以使用委托方案在VC0中设置关闭VC1代码。

我了解到,这是处理呈现和关闭的最省钱的方法-即使有时它可以在VC1本身内关闭VC1。

我问了一个非常相关的问题,您可能也有兴趣检查一下。它显示了代码...

ps我还读到一些仅关闭VC1-反过来也将关闭VC2。但是,如果我以前的建议可行,我不会这样做。有时我会在执行过程中(无崩溃)获得有关VC不再存在或与之相关的信息-因此我认为这不是最佳解决方案。但是,如果我以前的建议不起作用,您可以尝试第二个。

尽管不能保证这将持续更新到新的iOS,但由于此问题现在一直困扰着我一些iOS更新:-),因此我决定采用标准的推荐路线。

编辑:这是我修改后的代码-它可以正常工作,但尚不清楚您打算在用户响应警报后发生什么以及警报应该在VC1还是VC0上进行。无论如何使用委托和回调,我看不到任何问题。请解释一下我是否想念您的意思...

FlightViewControllerProtocol.h

@protocol FlightViewControllerProtocol <NSObject>
-(void) dismissVCAndEndBookmark;
@end
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

书签TableViewController.h

 @interface BookmarksTableViewController : UIViewController
{
    id delegate;
}

@property (nonatomic,strong)   id delegate;

@end
Run Code Online (Sandbox Code Playgroud)

书签TableViewController.m

- (IBAction)goBack {
    [self.delegate dismissVCAndEndBookmark];
}
Run Code Online (Sandbox Code Playgroud)

如果我正确理解了您的意图,在BookmarksTableViewController.m中的Esp回调似乎是实现中的主要问题。


Ray*_*oIL 5

[self.navigationController popViewControllerAnimated:YES];
Run Code Online (Sandbox Code Playgroud)

也为我做了把戏。以我为例,我有一个(iPhone)viewController,它使用push segue推送到堆栈上。由于启动segue的viewController具有导航栏,因此我必须向父控制器的navigationController发送popViewControllerAnimated消息,而不是调用它自己的dismissViewControllerAnimated:completion消息。

射线