Phill
Phill

Reputation: 5

Calling View from Class

I think I have a simple issue with my app. First of all I am using the PraseSDK in order to use the LoginView they offer.

Now I was going to create that function and let it called by a view controller in

-(void) viewDidLoad

It worked perfectly.

Now I was wondering if I can put that code into a global function class?

Well I created a Class called: glo_function Inside of it I created a function which is call

+(void) CallLoginScreen{
PFLogInViewController *login = [[PFLogInViewController alloc] init];
login.fields = PFLogInFieldsUsernameAndPassword | PFLogInFieldsLogInButton;
 [self presentModalViewController:login animated:NO];}

In My ViewController Iam using "callLoginScreen" like this

[glo_function CallLoginScreen]

The methode is going to be called by the View Controller but the view will not show up. Well I get that error will trying to run the app.

No known class method for selector 'presentModalViewController:animated:'

So I am pretty sure It do to the fact that I used the "self" in->

[self presentModalViewController:login animated:NO]

Can someone help me out with it? should be easy, hopefully :)

-----------------------Response------------------------------

Hi after I did that, the app crashes. The Call methode looks like that:

    [glo_function showLoginScreenUsingViewController];

In glo_function.m the methode looks like that:

+ (void)showLoginScreenUsingViewController:(UIViewController *)VC {
PFLogInViewController *login = [[PFLogInViewController alloc] init];
login.fields = PFLogInFieldsUsernameAndPassword | PFLogInFieldsLogInButton;
[VC presentModalViewController:login animated:NO];}

The Result when starting the App:

2012-12-28 21:20:24.003 Logbuch40[1942:c07] 
*** Terminating app due to uncaught exception 'NSInvalidArgumentException'
, reason: '+[glo_function showLoginScreenUsingViewController]: 
unrecognized selector sent to class 0x22e9ec

Upvotes: 0

Views: 146

Answers (1)

Dancreek
Dancreek

Reputation: 9544

"self" always refers to the object you are in. In your case it's the glo_function object. So when you say self you are talking to the wrong object.

You could still do what you want but in your glo_function method you need to pass in a reference to your view controller. Then just use that in your function instead of self.

+ (void)showLoginScreenUsingViewController:(UIViewController *)VC {
     PFLogInViewController *login = [[PFLogInViewController alloc] init];
     login.fields = PFLogInFieldsUsernameAndPassword | PFLogInFieldsLogInButton;
     [VC presentModalViewController:login animated:NO];
}

Upvotes: 2

Related Questions