我有很多类,例如 Page1、Page2、Page3、Page4、Page5 ...............
我想写一个方法去下一个类,它会根据我的需要调用Page1,2,3。
这是我正在做的代码:
for (int j = 0; j<5; j++) {
NSString *str_varForPage = [NSString stringWithFormat:@"%i", j];
if ([txt_Chapter.text isEqualToString:str_Chapter] && [txt_Page.text isEqualToString:str_varForPage]) {
NSString *Str_Page = @"Page";
NSString *Str_NextPage = [NSString stringWithFormat:@"%@%@", Str_Page , str_varForPage];
NSLog(@"Str_NextPage is: %@",Str_NextPage);
Str_NextPage *nextclass= [[Str_NextPage alloc]initWithNibName:nil bundle:nil]; // error Crash , I want to call Page0 to Page4 classes here according to my need.
[self.navigationController pushViewController:nextclass animated:YES];
}
我们非常欢迎任何想法或建议。
最佳答案
您可以像这样启动任何类的对象。
NSString *nextPage = @"Page1";
Page1 *nextclassInstance = [[[NSClassFromString(nextPage) alloc]initWithNibName:nil bundle:nil];
[self.navigationController pushViewController:nextclassInstance animated:YES];
关于ios - 如何在没有类名的情况下推送下一类而不是在 iOS 中使用 NSString?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21155403/