2

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

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

登录.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

登录.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

主控制器.h

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

@interface MainController : UITabBarController <LoginDelegate>

@end

主控制器.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的方法didFinishLaunchingOptions:

- (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 再次创建登录。我如何显示登录一次?当我尝试在 viewdidload 上执行此操作时,它不起作用。登录没有出现。

4

1 回答 1

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 委托显示登录控制器。

2)成功登录后,从登录控制器显示MainViewController。

于 2013-01-03T05:42:22.620 回答