23

我正在为我的 iOS 应用程序创建一个标题栏,并在 UIView 中创建它。我遇到的唯一问题是“主页按钮”。当home键被按下时,需要跳转到首页,也就是ViewController。

但是,它似乎不适[self presentViewController:vc animated:NO completion:NULL];用于 UIView。

我该如何解决这个问题?

- (void) createTitlebar {

    CGRect titleBarFrame = CGRectMake(0, 0, 320, 55);

    //Create the home button on the top right of the page. When clicked, it will navigate to the home page of the application
    homeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    homeButton.frame = CGRectMake(275, 10, 40, 40);
    [homeButton setTag:1];
    [homeButton setImage:[UIImage imageNamed:@"homeIcon.png"] forState:UIControlStateNormal];
    [homeButton addTarget:self action:@selector(homeButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:homeButton];
}

- (IBAction)homeButtonPressed:(id)sender{

    //Transition to the submit button page
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    ViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"ViewController"];
    [vc setModalPresentationStyle:UIModalPresentationFullScreen];
    [self presentViewController:vc animated:NO completion:NULL];
}
4

5 回答 5

39

您可以在不使用委托模式的情况下执行此操作。干得好

客观C

UIViewController *currentTopVC = [self currentTopViewController];
currentTopVC.presentViewController......... 

- (UIViewController *)currentTopViewController {
    UIViewController *topVC = [[[[UIApplication sharedApplication] delegate] window] rootViewController];
    while (topVC.presentedViewController) {
        topVC = topVC.presentedViewController;
    }
    return topVC;
}

迅速

var topVC = UIApplication.sharedApplication().keyWindow?.rootViewController
while((topVC!.presentedViewController) != nil) {
     topVC = topVC!.presentedViewController
}
topVC?.presentViewController........
于 2015-07-08T10:02:59.437 回答
17

只有 UIViewController 可以呈现另一个视图控制器,所以如果你需要从视图中显示一个视图控制器,有几种方法,其中一种是这样的:

制作一个视图控制器,您的父视图所在的视图控制器是它的代表

ParentView *view = [ParentView new];
...
view.delegate = self;

然后,在该委托的 ParentView 调用方法中

- (IBAction)homeButtonPressed:(id)sender {
    [self.delegate buttonPressed];
}

然后,在您的 VC 工具中

 -(void)buttonPressed {
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    ViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"ViewController"];
    [vc setModalPresentationStyle:UIModalPresentationFullScreen];
    [self presentViewController:vc animated:NO completion:NULL];
}

如果您需要将此代码保留在 UIView 中并避免委托,您可以做一个这样的技巧(我个人不喜欢它,但它应该可以工作)

-(void)buttonPressed{
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    ViewController *vc = [storyboard      instantiateViewControllerWithIdentifier:@"ViewController"];
    [vc setModalPresentationStyle:UIModalPresentationFullScreen];
    [(UIViewController*)self.nextResonder presentViewController:vc animated:NO completion:NULL];
}
于 2013-03-25T20:04:45.413 回答
4

这是 Shamsudheen 答案的更惯用的 Swift 3 版本:

extension UIApplication {

    static func topViewController() -> UIViewController? {
        guard var top = shared.keyWindow?.rootViewController else {
            return nil
        }
        while let next = top.presentedViewController {
            top = next
        }
        return top
    } 
}

然后你可以打电话:

UIApplication.topViewController()?.present(...)
于 2017-05-07T18:32:49.047 回答
2

使用 Swift 4 - 4.2 添加到 Shamsudheen TK 的答案。

var topVC = UIApplication.shared.keyWindow?.rootViewController
    while((topVC!.presentedViewController) != nil) {
        topVC = topVC!.presentedViewController
    }
    let customViewController = CustomViewController()
    topVC?.present(customViewController, animated: true, completion: nil)

使用 UINavigationController: 这里还有一个附加功能 -> 您可以将 UINavigationController 与您的 customViewController 一起传递。

 var topVC = UIApplication.shared.keyWindow?.rootViewController
    while((topVC!.presentedViewController) != nil) {
        topVC = topVC!.presentedViewController
    }
    let customViewController = CustomViewController()
    let navController = UINavigationController(rootViewController: CustomViewController)
    topVC?.present(navController, animated: true, completion: nil)
于 2019-01-30T07:24:57.657 回答
2

你可以试试下面的代码

 UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
 ViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"ViewController"];
 UIViewController *vc1 = [UIApplication sharedApplication].keyWindow.rootViewController;
 [vc1 presentViewController:vc animated:YES completion:nil]; 
于 2016-04-29T05:06:20.273 回答