2

I am a begginer with Objective-C programming. I searched here and in other websites to a way to solve my problem but i did not find. I want to create an login view before the system load. Then, after login i want to dismiss it. On my project i use ARC and not use Storyboards.

When i debug and look into the function "efetuarLogin" the value of property delegate is 0x000000. I think it is blank, is not? According to the tutorials and other tips i found on the internet, this is the right way to code delegate. Can you check this for me? Here is the code:

Login.h

#import <UIKit/UIKit.h>
@class Login;

@protocol LoginDelegate <NSObject>
@required
    - (void)loginDidFinish: (Login *)login;
@end

@interface Login : UIViewController{
    __weak id<LoginDelegate> delegate;
}

@property (nonatomic, weak) id <LoginDelegate> delegate;

- (IBAction)efetuarLogin:(id)sender;

@end

Login.M

#import "Login.h"

@implementation Login
@synthesize delegate;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)efetuarLogin:(id)sender {
    [delegate loginDidFinish:self];
}
@end

MainController.h

#import <UIKit/UIKit.h>
#import "Login.h"

@interface MainController : UITabBarController <LoginDelegate>

@end

MainController.m

#import "MainController.h"
#import "Login.h"
#import "ModalViews.h"
#import "Alerts.h"
#import "ViewPadrao.h"
#import "TableView.h"

@implementation MainController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    ModalViews *modalViews = [[ModalViews alloc] initWithNibName:@"ModalViews" bundle:nil];
    Alerts *alerts = [[Alerts alloc] initWithNibName:@"Alerts" bundle:nil];
    ViewPadrao *viewPadrao = [[ViewPadrao alloc] initWithNibName:@"ViewPadrao" bundle:nil];
    TableView *tableView = [[TableView alloc] initWithNibName:@"TableView" bundle:nil];

    self.viewControllers = @[modalViews, alerts, viewPadrao, tableView];
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];

    Login *login = [[Login alloc] initWithNibName:@"Login" bundle:nil];
    [login setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
    [self presentViewController:login animated:NO completion:nil];
}
- (void)loginDidFinish: (Login *)login{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Sua mensagem aqui" message:@"Mensagem" delegate:self cancelButtonTitle:@"Fechar" otherButtonTitles:nil];
    [alert show];
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

And the method didFinishLaunchingOptions of AppDelegate.m:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    mainController = [[MainController alloc] init];

    window.rootViewController = mainController;

    [window makeKeyAndVisible];

    return YES;
}

Sorry for the high quantity of code. I am thankful since already!!

I am using the ViewDidAppear method to show Login to users. But when i dismiss and the MainController appears, the method viewdidapper creates the Login again. How i do to show login once? When i tried to do this on viewdidload it did not work. The login did not appear.

  • Kaliman Borges, dont edit your original question with a problem which is entirely different to the original. Please post it as a different question, and re edit this to its original form. A question you asked here is for the community, so it should be readable even after many days. – Krishnabhadra Jan 03 '13 at 06:07

1 Answers1

2

In your MainViewController.m when you create login object

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];

    Login *login = [[Login alloc] initWithNibName:@"Login" bundle:nil];
    login.delegate = self;  //add this line
    [login setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
    [self presentViewController:login animated:NO completion:nil];
}

EDIT : After seeing OP's edit in the question (which should have been a different thread)

Your view Controller hierarchy is wrong. The ideal flow should be

1) Show Login controller directly from App delegate.

2) After successfull login, show MainViewController from Login Controller.

Krishnabhadra
  • 34,169
  • 30
  • 118
  • 167
  • Ok, worked! Thank you. I thought that would need declare a property on MainController to hold the Login object. – Káliman Borges Jan 03 '13 at 05:48
  • 1
    Glad it worked for you, the concept of creating delegate is discussed already in stackoverflow. When you get time, read [this](http://stackoverflow.com/questions/626898/how-do-i-create-delegates-in-objective-c) thread, which is very informative. And if this feel this answer is correct/solved the problem, please [accept it as answer](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work). BTW for a first question, yours is very well constructed. – Krishnabhadra Jan 03 '13 at 05:50
  • Thanks again Krishnabhadra. I always use Stackoverflow (only searching and reading) and it always solved many of my problems! Now i understood, delegates should be "initialized". BTW, when i dismiss the Login, it appears again. Due the implementation made on viewdidappear, but if i do this on viewdidload, Login not appear. after login i call this on the delegate method 'loginDidFinish' [self dismissViewControllerAnimated:YES completion:nil]; How i do to Login don`t appear again? – Káliman Borges Jan 03 '13 at 06:01