views:

195

answers:

3

I am just getting started with iphone development I have a Tabbed application and I wanted to display a log in form modally so i looked here Apple Dev and did this inside one of my view controllers I connected a button to the following action:

 #import "LoginForm.h"
...
-(IBAction)showLogin{
LoginForm *lf = [[LoginForm alloc]initWithNibName:@"LoginForm" bundle:nil];
lf.delegate = self;
lf.modalPresentationStyle =  UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:lf animated:YES];
}

when I build I get "request for member 'delegate' in something not a structure or union" If I get rid of the second line, it builds but pressing the button does nothing.

What am I missing here?

A: 

Does LoginForm have a "delegate" member?

jpmartineau
UIViewController
irco
thats a fantastic question...no it doesn't...I guess i kind of assumed it would get a delegate member from UIViewController...is there a protocol I need to fulfill? or a way to declare it?
irco
A: 

It would seem that your LoginForm class derives from UIViewController. The UIViewController class doesn't have a delegate property, hence the compile error you got.

Your problem is probably that the action doesn't get called in the first place. The proper signature for an action is:

- (IBAction)showLogin:(id)sender;

The sender argument is required. Put a breakpoint in your method to make sure it is called.

Martin Cote
how do i declare the delegate for the loginForm?and yes i think you are right i dont see the breakpoint being hit
irco
This is incorrect. An action method can take either zero parameters or one (the control sending it), and Interface Builder will happily hook a control up to an - (IBAction)doWhatever method.
Noah Witherspoon
+3  A: 

Sounds like you haven't declared a delegate member for LoginForm. You'll need to add code that lets the UIViewController instance that's presenting LoginForm modally when LoginForm is done. Here's how to declare your own delegate:

In LoginForm.h:

@class LoginForm;

@protocol LoginFormDelegate
- (void)loginFormDidFinish:(LoginForm*)loginForm;
@end

@interface LoginForm {
    // ... all your other members ...
    id<LoginFormDelegate> delegate;
}

// ... all your other methods and properties ...

@property (retain) id<LoginFormDelegate> delegate;

@end

In LoginForm.m:

@implementation

@synthesize delegate;

//... the rest of LoginForm's implementation ...

@end

Then in the UIViewController instance that presents LoginForm (let's call it MyViewController):

In MyViewController.h:

@interface MyViewController : UIViewController <LoginFormDelegate>

@end

In MyViewController.m:

/**
 * LoginFormDelegate implementation
 */
- (void)loginFormDidFinish:(LoginForm*)loginForm {
   // do whatever, then
   // hide the modal view
   [self dismissModalViewControllerAnimated:YES];
   // clean up
   [loginForm release];
}

- (IBAction)showLogin:(id)sender {
    LoginForm *lf = [[LoginForm alloc]initWithNibName:@"LoginForm" bundle:nil];
    lf.delegate = self;
    lf.modalPresentationStyle =  UIModalTransitionStyleCrossDissolve;
    [self presentModalViewController:lf animated:YES];
}
Art Gillespie
thanks so much...this is what i was looking for.The one last thing it's saying inside the protocol declaration is, that i can't figure out: "Expected ')' before LoginForm "i dont see much wrong with it. the only difference with your code is that my form is inheriting from UIViewController, but it doesnt look like is related to that error
irco
My bad... I forgot the `@class LoginForm;` before the protocol declaration. I've edited the source in my answer.
Art Gillespie
thanks, i also did an import in the MyViewController so that it could see the protocol, and it compiles but it still crashes before hitting the first line of the ShowLogin functionthe console shows an uncaught exception 'NSInvalidArgumentException', reason: '-[UIViewController showLogin]: unrecognized selector sent to instance 0x5936080'
irco
How are you calling `showLogin`? If it's by using a connection in Interface Builder, make sure you re-connect it now that the signature changed from `showLogin` in your original code to `showLogin:(id)sender` in the updated code.
Art Gillespie