gpt4 book ai didi

ios - 在其他类中的 dismissViewController 之后设置 UILabel 的文本

转载 作者:行者123 更新时间:2023-11-29 03:00:54 25 4
gpt4 key购买 nike

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

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

这是我的代码:

A类.h:

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

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

A 类:

shouldPerformSegue, prepareForSegue and statusBarStyle Methods

B类.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

最佳答案

这不起作用,因为在您的完成 block 中,您正在创建 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];
}

最后,您需要将 ShowProfileViewController 中的 delegate 属性设置为指向 LoginViewController 实例。在 LoginViewController.m 中找到您创建并显示 ShowProfileViewController View Controller 的代码部分,并将 delegate 属性设置为 self。如果您使用 Storyboard,则应该在 prepareForSegue: 中执行此操作。

关于ios - 在其他类中的 dismissViewController 之后设置 UILabel 的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23311146/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com