自定义委托iOS 6使用ARC无法正常工作

时间:2013-01-03 05:39:25

标签: objective-c ios xcode cocoa delegates

我是Objective-C编程的初学者。 我在这里和其他网站搜索了一种方法来解决我的问题,但我没有找到。 我想在系统加载之前创建一个登录视图。然后,登录后我想解雇它。 在我的项目中,我使用ARC而不使用Storyboard。

当我调试并查看函数“efetuarLogin”时,属性委托的值为0x000000。我觉得它是空白的,不是吗? 根据我在互联网上找到的教程和其他技巧,这是代理委托的正确方法。你能帮我查一下吗? 这是代码:

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

该方法确实是AppDelegate.m的FininLaunchingOptions:

- (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;
}

抱歉代码量很大。我已经感恩了!!

我正在使用ViewDidAppear方法向用户显示登录信息。但是当我解雇并出现MainController时,方法viewdidapper再次创建Login。 如何显示登录一次?当我试图在viewdidload上执行此操作时它不起作用。登录没有出现。

1 个答案:

答案 0 :(得分:2)

创建MainViewController.m对象

时的login
- (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];
}

编辑:在问题中看到OP的编辑后(应该是一个不同的主题)

您的视图控制器层次结构错误。理想的流程应该是

1)直接从App delegate显示登录控制器。

2)成功登录后,从Login Controller显示MainViewController。