Ash*_*wal 21 xcode objective-c ios ios6 presentviewcontroller
我正在为我的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];
}
Run Code Online (Sandbox Code Playgroud)
Sha*_* TK 38
您可以在不使用委托模式的情况下执行此操作.干得好
的ObjectiveC
UIViewController *currentTopVC = [self currentTopViewController];
currentTopVC.presentViewController.........
- (UIViewController *)currentTopViewController {
UIViewController *topVC = [[[[UIApplication sharedApplication] delegate] window] rootViewController];
while (topVC.presentedViewController) {
topVC = topVC.presentedViewController;
}
return topVC;
}
Run Code Online (Sandbox Code Playgroud)
迅速
var topVC = UIApplication.sharedApplication().keyWindow?.rootViewController
while((topVC!.presentedViewController) != nil) {
topVC = topVC!.presentedViewController
}
topVC?.presentViewController........
Run Code Online (Sandbox Code Playgroud)
ale*_*lex 17
只有UIViewController可以呈现另一个视图控制器,所以如果你需要从视图中显示一个viewcontroller有几种方法,其中一种是这样的:
创建一个viewcontroller,您的父视图位于该视图控制器的委托上
ParentView *view = [ParentView new];
...
view.delegate = self;
Run Code Online (Sandbox Code Playgroud)
然后,在该委托的ParentView调用方法中
- (IBAction)homeButtonPressed:(id)sender {
[self.delegate buttonPressed];
}
Run Code Online (Sandbox Code Playgroud)
然后,在你的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];
}
Run Code Online (Sandbox Code Playgroud)
如果你需要在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];
}
Run Code Online (Sandbox Code Playgroud)
这是 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
}
}
Run Code Online (Sandbox Code Playgroud)
然后你就可以调用:
UIApplication.topViewController()?.present(...)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
24547 次 |
| 最近记录: |