目标C使vc1的UITextField成为UILabel vc2

时间:2016-02-26 05:58:46

标签: ios objective-c iphone

我想在我的第一个视图控制器中获取我的UITextField电子邮件,成为我的下一个视图控制器中的UILabel。它说有一个错误声明NSString没有可见的@interface声明了选择器initvalue。我认为当你在vc2上导入它时,从vc1.h文件加载了界面?你能帮忙的话,我会很高兴。这是我到目前为止所拥有的:

vc1.h

 #import <UIKit/UIKit.h>

@interface loginUserViewController : UIViewController


@property (nonatomic, retain, strong) IBOutlet UITextField *email;
@property (nonatomic, retain) IBOutlet UITextField *password;
@property (nonatomic, retain) IBOutlet UIButton *login;
@property (nonatomic,retain) IBOutlet UIButton *registerBtn;


-(IBAction)loginUser:(id)sender;





@end

vc2.h

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


@interface Home : UIViewController


@property (weak, nonatomic) IBOutlet UILabel *username;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *Nav;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *logout;


-(IBAction)logout:(id)sender;

-(IBAction)bandHome:(id)sender;

@end

vc2.m

import "Home.h"
#import "loginUserViewController.h"


@interface Home ()

@end

@implementation Home
@synthesize username, band1, band2, band3;

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

    }
    return self;
}

-(void)viewDidLoad
{
    [super viewDidLoad];
    loginUserViewController *object = [[loginUserViewController alloc]init];
    NSString *string = [[NSString alloc] initWithFormat:@"%i",[object.email.text initValue]];

    username.text = string;
}

vc1.m

#import "loginUserViewController.h"
#import "Home.h"
#import "createBandViewController.h"

@interface loginUserViewController ()


@end

@implementation loginUserViewController

@synthesize email, password;
- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

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

}

- (void)viewDidUnload
{

    [self setEmail:nil];
    [self setPassword:nil];

    [super viewDidUnload];
}
-(IBAction)loginUser:(id)sender {
    if ([email.text isEqualToString:@""] || [password.text isEqualToString:@""])

    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"alert" message:@"Please Fill all the field" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
        return;
    }

2 个答案:

答案 0 :(得分:3)

只需替换此字符串:

NSString *string = [[NSString alloc] initWithFormat:@"%i",[object.email.text initValue]];

使用此字符串:

NSString *string = [[NSString alloc] initWithFormat:@"%i",[object.email.text intValue]];

如果您只想将电子邮件作为字符串,则将其替换为此字符串

NSString *string = [[NSString alloc] initWithFormat:@"%@",object.email.text];

你的错误将被解决。希望它有帮助:)

答案 1 :(得分:0)

首先在VC2.h中

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


@interface Home : UIViewController


@property (strong, nonatomic) IBOutlet UILabel *username; // Keep property strong to get the value 
VC2.m

中的

@synthesize username;

现在在VC1.h

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

@interface loginUserViewController : UIViewController


@property (nonatomic, strong) IBOutlet UITextField *email;
@property (nonatomic, strong) IBOutlet UITextField *password;
@property (nonatomic) nsstring *username;

-(void)viewDidLoad
{
    [super viewDidLoad];
    username= self.email.text;
    homeviewcontroller *object = [[homeviewcontroller alloc]init];
     object.username.text=username;

   }