NSString과 Class의 상호 전환
2261 단어 ios 플랫폼
Class class = NSClassFromString(self.arr[sender.tag - 100]);
객체 전환 문자열
VC.navigationItem.title = NSStringFromClass([VC class]);
전체 코드
- (void)viewDidLoad {
[super viewDidLoad];
self.arr = @[@"ViewController1", @"ViewController2", @"ViewController3", @"ViewController4"];
}
- (IBAction)buttonAction:(UIButton *)sender {
NSLog(@"--%@--", [NSString stringWithFormat:@"%ld", sender.tag]);
//
Class class = NSClassFromString(self.arr[sender.tag - 100]);
UIViewController *VC = [(UIViewController *)[class alloc] init];
VC.view.backgroundColor = [UIColor whiteColor];
//
VC.navigationItem.title = NSStringFromClass([VC class]);
[self.navigationController pushViewController:VC animated:YES];
}