ShowProfileViewController

ShowProfileViewController

我还没有找到类似的问题可以回答我的问题。

我的问题是:为什么在dissmissViewController之后不能从另一个类访问UILabel?

这是我的代码:

Class.h:

@interface ClassA : UIViewController {
    UILabel *_ErrorLabel;
    UIActivityIndicatorView *_acIn1;
}

@property (weak, nonatomic) IBOutlet UILabel *ErrorLabel;
@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *acIn1;


A.m:

shouldPerformSegue, prepareForSegue and statusBarStyle Methods


ClassB.h:

- (IBAction)dismiss;


B.m:

- (IBAction)dismiss
{
  [self dismissViewControllerAnimated:YES completion:^{
      ClassA *login = [[ClassA alloc] init];
      [[login ErrorLabel] setText:@"Please use login."];
      [[login acIn1] stopAnimating];
      [[login acIn1] setHidesWhenStopped:YES];
      [[login acIn1] setHidden:YES];
  }];
}


这是我的代码,我真的希望有人能帮助我:我要放弃,我不知道这为什么行得通!

谢谢你的帮助。

〜马库斯

编辑1:

我有一个包含两个文本字段的ViewController ClassA,当您单击登录时,您进入一个TabBarController,其中一个选项卡包含ClassB ViewController,并且在ClassB ViewController中有一个注销按钮->关闭,当您单击此按钮时,您应该到ClassA ViewController,并且ErrorLabel文本应更改。

完整的类:A-> LoginViewControler.h

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

@interface LoginViewController : UIViewController <ShowProfileViewControllerDelegate> {
    UILabel *_ErrorLabel;
    UIActivityIndicatorView *_acIn1;
}

@property (weak, nonatomic) IBOutlet UITextField *usernameTextField;
@property (weak, nonatomic) IBOutlet UITextField *passwordTextField;
@property (weak, nonatomic) IBOutlet UILabel *ErrorLabel;
@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *acIn1;

@end


完整的类:A-> LoginViewController.m

#import "LoginViewController.h"
#import "NewsNavigationController.h"
#import "TabViewController.h"

@interface LoginViewController () <UITextFieldDelegate>

@end

@implementation LoginViewController

@synthesize usernameTextField;
@synthesize passwordTextField;
@synthesize ErrorLabel;
@synthesize acIn1;

- (void)viewDidLoad
{
    [super viewDidLoad];
    [usernameTextField setDelegate:self];
    [passwordTextField setDelegate:self];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}

- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender
{

    if([identifier isEqualToString:@"login"])
    {
        [acIn1 startAnimating];
        [acIn1 setHidden:NO];
        if([self login]){
            return YES;
        } else {
            [self showErrorMessage:@"Data not correct!"];
            [acIn1 stopAnimating];
            [acIn1 setHidesWhenStopped:YES];
            [acIn1 setHidden:YES];
            return NO;
        }
    }
    else {
        [acIn1 stopAnimating];
        [acIn1 setHidesWhenStopped:YES];
        [acIn1 setHidden:YES];
        return NO;
    }
}

- (void)showErrorMessage:(NSString *)message
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error!"
                                                    message:message
                                                    delegate:self
                                                    cancelButtonTitle:@"OK"
                                                    otherButtonTitles:nil];
    [alert show];
}

- (BOOL)login
{
    NSString *usernameS = usernameTextField.text;
    NSString *passwordS = passwordTextField.text;

    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://localhost:8888/login.php?username=%@&password=%@", usernameS, passwordS]]];

    NSDictionary *jsonDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];

    NSDictionary *loginDic = [jsonDictionary objectForKey:@"login"];

    NSString *ErrorString = [loginDic objectForKey:@"returnString"];

    NSLog(@"[+] Login: %@", ErrorString);

    if ([ErrorString isEqualToString:@"Success"]){
        ErrorLabel.text = @"Login";
        return YES;
    }
    else {
        ErrorLabel.text = ErrorString;
        return NO;
    }

}

- (void)didDismissViewController
{
    [ErrorLabel setText:@"Bitte benutzen Sie den Login."];
    [acIn1 stopAnimating];
    [acIn1 setHidesWhenStopped:YES];
    [acIn1 setHidden:YES];
}

- (void)prepareForSegue:(UIStoryboardSegue *)inSegue sender:(id)inSender
{
    if([inSegue.identifier isEqualToString:@"login"])
    {
        ShowProfileViewController *vc = [[ShowProfileViewController alloc] init];
        vc.delegate = self;
        TabViewController *tabViewController = inSegue.destinationViewController;
        NewsNavigationController *theController = [[tabViewController viewControllers] objectAtIndex:0];
        [self presentViewController:vc animated:YES completion:nil];
    }
}


@end


完整的类:B-> ShowProfileViewController.h

#import <UIKit/UIKit.h>

@protocol ShowProfileViewControllerDelegate

- (void)didDismissViewController;

@end

@interface ShowProfileViewController : UIViewController

@property (nonatomic, assign) id<ShowProfileViewControllerDelegate> delegate;

- (IBAction)dismiss;

@end


完整的课程:B-> ShowProfileViewController.m

#import "ShowProfileViewController.h"
#import "LoginViewController.h"

@interface ShowProfileViewController ()

@end

@implementation ShowProfileViewController


- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

-(BOOL) textFieldShouldReturn:(UITextField *)textField{

    [textField resignFirstResponder];
    return YES;
}

- (void)viewWillAppear:(BOOL)inAnimated
{
    [super viewWillAppear:inAnimated];
}

- (IBAction)dismiss
{
    [self dismissViewControllerAnimated:YES completion:^{
        [self.delegate didDismissViewController];
    }];
}

@end

最佳答案

这不起作用,因为在完成块中,您正在创建LoginViewController的新实例并设置其文本。您实际上应该做的是设置现有LoginViewController的文本,该文本应在关闭ShowProfileViewController后显示

为了实现所需的行为,可以使用delegation pattern。如果您不熟悉此技术,那么学习非常重要。它在iOS和Mac OS X开发中被广泛使用。

下面的代码可能需要您进行一些调整。

ShowProfileViewController.h中,在@interface之前添加:

@protocol ShowProfileViewControllerDelegate

- (void)didDismissViewController

@end


另外,将以下属性声明添加到ShowProfileViewController

@property (nonatomic, assign) id<ShowProfileViewControllerDelegate> delegate;


然后,更改LoginViewController.h,使其看起来像

#import "ShowProfileViewController.h"

@interface LoginViewController : UIViewController <ShowProfileViewControllerDelegate> {
    UILabel *_ErrorLabel;
    UIActivityIndicatorView *_acIn1;
}

@property (weak, nonatomic) IBOutlet UILabel *ErrorLabel;
@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *acIn1;


现在,在ShowProfileViewController.m中,替换dismiss方法中的代码,如下所示:

- (IBAction)dismiss
{
  [self dismissViewControllerAnimated:YES completion:^{
      [self.delegate didDismissViewController];
  }];
}


LoginViewController.m中,添加以下方法:

- (void)didDismissViewController
{
  [[self ErrorLabel] setText:@"Please use login."];
  [[self acIn1] stopAnimating];
  [[self acIn1] setHidesWhenStopped:YES];
  [[self acIn1] setHidden:YES];
}


最后,您需要在delegate中设置ShowProfileViewController属性以指向LoginViewController实例。在LoginViewController.m中查找您在代码的哪一部分中创建并显示ShowProfileViewController视图控制器,并将delegate属性设置为self。如果使用情节提要板,则应在prepareForSegue:中执行。

10-05 18:13