【问题标题】:dismissViewController: not working解雇视图控制器:不工作
【发布时间】:2012-09-29 04:10:18
【问题描述】:

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

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

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

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

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

[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 中替换 dismissViewControllerAnimated: 以手动呈现 vc0,但随后我在控制台中收到一条日志,说我正在尝试呈现 vc0 但 vc1 的视图不在窗口层次结构中。这是什么意思?

感谢您的帮助!

更新:

在这种情况下,VC0 是 MenuMileIndexViewController - VC1 是 FlightViewController - VC2 是 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];
    }];
}

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

【问题讨论】:

  • 如何以及在哪里设置按钮的操作?
  • 1. “这是什么意思?”:这是因为您正在通过“手动呈现vc0”创建一个新的vc0实例。 2.“//这里我调用vc1中的一个方法”怎么办?

标签: iphone objective-c ios xcode viewcontroller


【解决方案1】:

我建议始终从实际呈现它的 VC 中解散 VC - 使用委托。这实际上也是 Apple 推荐的方式 - 正如我之前对这个问题的问题的回答中所指出的那样。

因此,如果您有 VC0 呈现 VC1,则在 VC0 中也有关闭 VC1 代码,使用委托方案。

我了解到这是处理呈现和解除的最省钱的方法 - 尽管有时它可以在 VC1 本身内解除 VC1。

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

ps 我还读到有些人只解雇 VC1 - 这反过来也会解雇 VC2。但是,如果我之前的建议有效,我不会这样做。有时我在执行过程中得到信息(没有崩溃),VC 不再存在或与之相关的任何东西 - 所以我认为这不是最好的解决方案。但是如果我之前的建议不起作用,你可以试试第二个。

虽然不能保证这将持续到新 iOS 的更新,因为这个问题一直困扰着我,现在要更新几个 iOS :-),所以我决定走标准推荐路线。

编辑: 这是我修改后的代码——它可以正常工作——但是不清楚在用户响应警报之后你打算发生什么以及警报应该在 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 中的回调似乎是您实现中的主要问题。

【讨论】:

  • 这个问题的答案,正如你所引用的'旧的好方法'就是我正在做的......但为什么它不起作用
  • 你正在做:[self dismissViewControllerAnimated:YES completion: ^{ // 在这里我在 VC2 中调用 vc1 中的一个方法 } 而不是像 [delegate dismissVC] 这样在 VC1 中执行的方法。还可以尝试动画:否 - 但请确保您确实从 VC1 中解散 VC2 并从 VC0 中解散 VC1
  • 为什么我应该使用委托? Apple 为我提供了一个完成块......我会尝试动画 NO
  • 我没有为此使用完成块,因为我在某处读到执行顺序,即 FINISHED 被解雇的 VC 不能保证在调用块中的代码之前真正完成。在您的情况下可能也是如此-除了使用代表我没有想法:-(。ps还尝试在iOS发布代码版本中使用您的应用程序,因为它的行为与调试版本代码略有不同-它执行得更快,因此可能导致执行顺序不同(就像我的情况一样)
  • 其实我已经测试过了。我在 viewDidLoad 中放了一个 NSLog,在 vc1 中由 vc2 中的完成块调用的方法,首先调用 viewDidLoad
【解决方案2】:
[self.navigationController popViewControllerAnimated:YES];

也为我做了诀窍。就我而言,我有一个(iPhone)viewController 使用 push segue 被推送到堆栈中。由于启动 segue 的 viewController 有一个导航栏,我不得不向父控制器的 navigationController 发送 popViewControllerAnimated 消息,而不是调用它自己的 dismissViewControllerAnimated:completion 消息。

【讨论】:

  • 我一直认为 popViewController: 如果你使用了 pushViewController: 会返回,在这种情况下你必须有一个导航控制器来做这一切——我没有
  • 是的。抱歉,我错过了您原始帖子中的具体细节。我试图指出,使用 segues,您可能在不知不觉中处于 NavigationController 之下,因此 pop 可能是方法而不是驳回。
【解决方案3】:

第一个视图:

首先在第一个视图中嵌入导航控制器。 并使用此代码导航到另一个视图

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

第二个视图:

在关闭视图的方法中添加这一行

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

【讨论】:

  • 为什么需要创建导航控制器?
  • @MCKapur,让对象管理导航而不是手动尝试管理每个部分被认为是最佳实践。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-18
  • 2016-06-30
  • 1970-01-01
  • 2017-09-10
  • 1970-01-01
相关资源
最近更新 更多