Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

present modal view controller

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?

like image 476
irco Avatar asked Jul 21 '10 23:07

irco


People also ask

What is present Modally?

Present Modally - Presents a view controller overtop the current view controller in various fashions as defined by the modal presentation and transition style - most commonly used to present a view controller in a sheet that animates up from the bottom. Example: Selecting Face ID & Passcode in Settings.

What is a UIViewController?

The UIViewController class defines the shared behavior that's common to all view controllers. You rarely create instances of the UIViewController class directly. Instead, you subclass UIViewController and add the methods and properties needed to manage the view controller's view hierarchy.


1 Answers

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];
}
like image 75
Art Gillespie Avatar answered Oct 27 '22 17:10

Art Gillespie