如何在NSString对象的objective-c中分配一个类?

时间:2022-09-07 07:41:18

I've got a PHP background, but I'm beginning to learn Objective-C, so that I can develop apps for the iPhone. So far things are going pretty well, but I've got a question that I haven't been able to find and answer to yet after googling and mining a number of different forums.

我有一个PHP背景,但我开始学习Objective-C,以便我可以为iPhone开发应用程序。到目前为止事情进展顺利,但我有一个问题,在google搜索并挖掘了许多不同的论坛之后,我还无法找到并回答这个问题。

My app has to create a number of views, each with it's own uinque title bar. In order to do this, my code looks something like this for each view:

我的应用程序必须创建一些视图,每个视图都有自己的uinque标题栏。为了做到这一点,我的代码对于每个视图看起来都是这样的:

    xViewController = [ [ XViewController alloc ] init ];
    xNavController = [ [ UINavigationController alloc ]
        initWithRootViewController: xViewController
    ];
    xNavController.tabBarItem = [ [ UITabBarItem alloc ]
        initWithTitle: @"My Info"
        image: [ UIImage imageNamed: @"my_info.png" ]
        tag: 3
    ];  

This works, but what I'd like to do is to create a method that will return a nav controller when sent a string as a message, so I don't have to do all this for each view. The issue I am having is that the first line needs to allocate an object based on a class name passed to it as a string (i.e. XViewController needs to be taken from a string passed to the method), but I don't know how to treat a string as a class name. I know it's possible, because the UIApplicationMain() can use a string to call the app delegate class. How can I do it?

这样做,但我想要做的是创建一个方法,当一个字符串作为消息发送时将返回一个导航控制器,所以我不必为每个视图做所有这些。我遇到的问题是第一行需要根据作为字符串传递给它的类名来分配一个对象(即XViewController需要从传递给该方法的字符串中获取),但我不知道如何将字符串视为类名。我知道这是可能的,因为UIApplicationMain()可以使用字符串来调用app delegate类。我该怎么做?

I'm sorry if any of this doesn't make sense, I'm still in the early stages of learning a new language!

我很抱歉,如果这一点没有意义,我还处于学习新语言的早期阶段!

4 个解决方案

#1


As well as using the runtime, you can do it just using Cocoa-touch methods:

除了使用运行时,您只需使用Cocoa-touch方法即可:

NSString* theClassName = /* assume this exists */
Class theClass = NSClassFromString(theClassName);
NSObject* myObject = [[theClass performSelector:@selector(alloc)] init];
// do something with the new object instance

#2


If you have multiple TableViewCells loaded from NIBs, the code to load the individual NIBs gets ugly if you don't do something with dynamic loading.

如果您有多个从NIB加载的TableViewCells,如果您不对动态加载执行某些操作,则加载单个NIB的代码会变得很难看。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // header
    if (indexPath.row == 0) {
        cellHeader *cell = (cellHeader *)[self loadCell:identHeader];
        return cell;

    // load more
    } else if (indexPath.row == lastRow) {
        loadMore *cell = (loadMore *)[self loadCell:identLoadMore];
        return cell;
    }

    // default
    cellDefault *cell = (cellDefault *)[self loadCell:identDefault];
    return cell;
}

- (id)loadCell:(NSString *)className {
    id cell = [tweetsView dequeueReusableCellWithIdentifier:className];

    if (cell == nil) {
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:className owner:nil options:nil];

        for (id currentObject in topLevelObjects) {
            if ([currentObject isKindOfClass:[UITableViewCell class]]) {
                cell = currentObject;
                break;
            }
        }
    }

    return cell;
}

#3


Use Objective-C 2.0 Runtime Reference

使用Objective-C 2.0运行时参考

#4


If I understand your problem correctly you don't need dynamic classes to make this work. Why not create, for each view controller class, its unique title and image method that just returns the unique title and image for that view controller. Then create a super class for your view controller classes with dummy title and image methods.

如果我正确理解您的问题,您不需要动态类来使其工作。为什么不为每个视图控制器类创建其唯一的标题和图像方法,它只返回该视图控制器的唯一标题和图像。然后使用虚拟标题和图像方法为视图控制器类创建一个超类。

Your code above will then look something like this:

您上面的代码将看起来像这样:

- (UINavigationController *)getNavController:(XGenericViewController *)viewController
{
        UINavigationController  *xNavController = [ [ UINavigationController alloc ]
            initWithRootViewController: viewController
        ];
        xNavController.tabBarItem = [ [ UITabBarItem alloc ]
            initWithTitle: [viewController title]
            image: [viewController image]
            tag: 3
        ];

    return [xNavController autorelease];
}

#1


As well as using the runtime, you can do it just using Cocoa-touch methods:

除了使用运行时,您只需使用Cocoa-touch方法即可:

NSString* theClassName = /* assume this exists */
Class theClass = NSClassFromString(theClassName);
NSObject* myObject = [[theClass performSelector:@selector(alloc)] init];
// do something with the new object instance

#2


If you have multiple TableViewCells loaded from NIBs, the code to load the individual NIBs gets ugly if you don't do something with dynamic loading.

如果您有多个从NIB加载的TableViewCells,如果您不对动态加载执行某些操作,则加载单个NIB的代码会变得很难看。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // header
    if (indexPath.row == 0) {
        cellHeader *cell = (cellHeader *)[self loadCell:identHeader];
        return cell;

    // load more
    } else if (indexPath.row == lastRow) {
        loadMore *cell = (loadMore *)[self loadCell:identLoadMore];
        return cell;
    }

    // default
    cellDefault *cell = (cellDefault *)[self loadCell:identDefault];
    return cell;
}

- (id)loadCell:(NSString *)className {
    id cell = [tweetsView dequeueReusableCellWithIdentifier:className];

    if (cell == nil) {
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:className owner:nil options:nil];

        for (id currentObject in topLevelObjects) {
            if ([currentObject isKindOfClass:[UITableViewCell class]]) {
                cell = currentObject;
                break;
            }
        }
    }

    return cell;
}

#3


Use Objective-C 2.0 Runtime Reference

使用Objective-C 2.0运行时参考

#4


If I understand your problem correctly you don't need dynamic classes to make this work. Why not create, for each view controller class, its unique title and image method that just returns the unique title and image for that view controller. Then create a super class for your view controller classes with dummy title and image methods.

如果我正确理解您的问题,您不需要动态类来使其工作。为什么不为每个视图控制器类创建其唯一的标题和图像方法,它只返回该视图控制器的唯一标题和图像。然后使用虚拟标题和图像方法为视图控制器类创建一个超类。

Your code above will then look something like this:

您上面的代码将看起来像这样:

- (UINavigationController *)getNavController:(XGenericViewController *)viewController
{
        UINavigationController  *xNavController = [ [ UINavigationController alloc ]
            initWithRootViewController: viewController
        ];
        xNavController.tabBarItem = [ [ UITabBarItem alloc ]
            initWithTitle: [viewController title]
            image: [viewController image]
            tag: 3
        ];

    return [xNavController autorelease];
}