我非常擅长使用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
答案 0 :(得分:0)
您必须使用-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];
}