23

iOSアプリケーションのタイトルバーを作成しており、これをUIView内で作成しています。私が抱えている唯一の問題は「ホームボタン」です​​。ホームボタンを押すと、ホームページ(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

デリゲートパターンを使用せずにこれを行うことができます。どうぞ

ObjectiveC

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のみが別のViewControllerを表示できるため、ViewからViewControllerを表示する必要がある場合は、いくつかの方法があり、そのうちの1つは次のようになります。

親ビューが配置されているViewControllerをそのデリゲートにします

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の答えのより慣用的なSwift3バージョンは次のとおりです。

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

以下のコードを試すことができます

 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 に答える
2

Swift4 で-4.2ShamsudheenTKの答えに追加します。

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

UINavigationControllerの場合: 追加機能もあります->customViewControllerでUINavigationControllerを渡すことができます。

 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 に答える