views:

292

answers:

2

according to this code i dont understand about NSClassFromString

how did i know "viewControllerName" can i use it from another source ?

could you please tell me more information how to use NSClassFromString?

UIViewController *viewController = [[NSClassFromString(viewControllerName) alloc] init];
+4  A: 

NSClassFromString returns an Objective-C class based on it's name as a string (The function name is really self explanitory)...

The string you pass in is the name of the class you want to create, for example:

If you have a class called MyViewController and you want to get an Objective-C class from that string, you use this function, like NSClassFromString(@"MyViewController");

Normally you only need to do this when testing for class names that may not be available to you, for example, when writing code that targets iPhone OS 2.0 and 3.0 simultaneously.

If you use the class as a symbol, it'll crash if the class doesn't exist on the previous version of the OS. So you use this function to return a class using that string, and if the class is successfully returned, you know that it exists and is safe to use, else if it returns nil, then the class doesn't exist, you shouldn't use it and your program should take steps to behave properly in this situation.

Jasarien
thanks Jasarien , can you tell me. where i should to define my custom class? can i grab custom class from another source eg, from xml ?thanks for help if yes, could you show me some simple algorithm?
RAGOpoR
If you're working with custom classes, i.e classes that you create and are part of your project, you have no reason to use NSClassFromString... Why do you want to use this function? As long as your class declaration is correct, and the header is imported where you need to use it, you can simply use the class name literally, as you do with other classes, like NSString.
Jasarien
i see, that mean that class must not a part of project right?a little question,so @"MyViewController" is that mean #import "MailComposerViewController.h"@implementation MailComposerViewController...@endor it mean MailComposerViewControllerif it mean MailComposerViewController i dont understand how can it build a class because i dont know where is the code of that class.
RAGOpoR
I'm not sure what you're trying to ask.You said you were using a custom class, but then you mention the mail composer class... Its very confusing. The `MFMailComposerViewController` belongs to the MessageUI framework, which you will need to include in your project and link against if you wish to use it. You only need to worry about the OS compatibility checking if you're going to support users that have versions prior to iPhone OS 3.0. If you are only going to support 3.x and upwards, you don't need to worry about it.
Jasarien
@RAGOpoR: It sounds like you are trying to understand the MailComposer example application from Apple. That project is designed to show how to use OS 3.x features while targeting 2.x. They weak-link in the MessageUI framework, where MFMailComposeViewController is defined, and use NSClassFromString to check if MFMailComposeViewController exists on the OS the application is running on. If so, they proceed with using it.
Brad Larson
Is I need to make a new question? I want to use XML or Json to told my app. I want to make a dynamic application that will change a feature by passing class from XML.
RAGOpoR
i think i should make a new question
RAGOpoR
new thread at herehttp://stackoverflow.com/questions/2467291/is-it-possible-to-make-a-dynamic-application-with-nsclassfromstring
RAGOpoR
A: 

I have a situation in which I wish the class name to be determined at runtime, and so NSClassToString is useful. However, when using those classes from a static library it returns nil. I've had this issue before and solved it using the -ObjC linker flag.

However, for some reason in my current project this doesn't solve it. The only difference I can see is that in the first case (following a tutorial) I made an OSX static library, and changed the relevant parts to make it produce an ARMv6 version. In the second case I created a static library for iPhone OS, from the template.

It is indeed a puzzlement.


Edit

I found the solution, so here it is: I simply added the -ObjC linker flag to the target that used the static library as well as the target for the library itself.

Justy