【问题标题】:Push a button and have a random viewcontroller?按下按钮并拥有一个随机的视图控制器?
【发布时间】:2014-07-19 16:22:20
【问题描述】:

我对使用 Xcode 在 Objective-C 中进行编码非常陌生,我试图能够按下按钮并让代码拉出一个随机视图控制器。

我为每个视图控制器创建了类并分配了它们。

我的 viewcontroller.m:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

-(IBAction)randomviewbutton {

    int randomview = rand() % 9;
    switch (randomview) {
        case 0:
            change.modalViewController = [UIViewController class:@"ViewController1"];
            break;
        case 1:
            change.modalViewController = [UIViewController class:@"ViewController2"];
            break;
        case 2:
            change.modalViewController = [UIViewController class:@"ViewController3"];
            break;
        case 3:
            change.modalViewController = [UIViewController class:@"ViewController4"];
            break;
        case 4:
            change.modalViewController = [UIViewController class:@"ViewController5"];
            break;
        case 5:
            change.modalViewController = [UIViewController class:@"ViewController6"];
            break;
        case 6:
            change.modalViewController = [UIViewController class:@"ViewController7"];
            break;
        case 7:
            change.modalViewController = [UIViewController class:@"ViewController8"];
            break;
        case 8:
            change.modalViewController = [UIViewController class:@"ViewController9"];
            break;
        default:
            break;
    }
}

我的 viewcontroller.h:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController {

    IBOutlet UIViewController *change;
}

-(IBAction)randomviewbutton;

@end

【问题讨论】:

    标签: ios uiviewcontroller uibutton


    【解决方案1】:

    您必须使用-presentViewController:animated:completion: 呈现模态视图控制器,而不是直接更改modalViewController 属性。

    类似这样的:

    -(IBAction)randomviewbutton {
        UIViewController *vc = nil;
        int randomview = rand() % 9;
        switch (randomview) {
            case 0:
                vc = [[ViewController1 alloc] init];
            case 1:
                vc = [[ViewController2 alloc] init];
            default:
                break;
        }
        [change presentViewController:vc animated:YES completion:nil];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多