ios - 如何在没有类名的情况下推送下一类而不是在 iOS 中使用 NSString?

标签 ios iphone objective-c ipad

我有很多类,例如 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/

相关文章:

ios - UIRefreshControl 在 UITableViewController 中的位置错误

ios - 在 Swift 中以编程方式从主 TabBarController 转到嵌套的 ViewController

iOS pdf 加密 256 位 AES

iphone - NSDate、NSCalendar 和 NSDateComponents 计时

iphone - 同时使用 vibrate 和 AVCaptureSession

ios - 向服务器发送简单的 POST 消息

ios - 是不是JSON解析错了?

ios - 如何在 ios 中管理 facebook 集成中的 session

iphone - 图像未使用 UIImage drawInRect 缩放? (见附件代码)

objective-c - 小图标应用程序 ListView osx