枚举值未正确显示

时间:2016-02-01 11:06:40

标签: ios objective-c enums

在我的A班。我正在使用Enum。

typedef enum {
    EditProfile,
    CreateProfile,
} AdressState;

@interface ManageAddress : UIViewController<UITableViewDataSource,UITableViewDelegate,UIGestureRecognizerDelegate>
{
    AdressState state;
}

并在.m文件中分配值

-(IBAction)adress:(id)sender
{
    state = CreateProfile;
    MyAddress *my = [[MyAddress alloc]initWithNibName:@"MyAddress" bundle:nil];
    [self.navigationController pushViewController:my animated:YES];
}

在我的MyAddressController我正在访问枚举值

@interface MyAddress : UIViewController<UITextFieldDelegate,UITextViewDelegate>
{
    AdressState state;
}

.m文件

if (state == EditProfile )
{
    mobile.text = [[NSUserDefaults standardUserDefaults]objectForKey:@"mobile"];
}

我的状态显示始终为EditProfile但我分配了一个值CreateProfile。请帮忙。

2 个答案:

答案 0 :(得分:1)

您需要将ManageAddress的{​​{1}}成员变量中的值分配给state MyAddress成员变量的值,因为它们是两个都在不同的班级。只需将您的state方法更新为以下内容:

(IBAction)adress:(id)sender

}

答案 1 :(得分:0)

在AppDelegate或Singleton类中全局创建enum,然后从ManageAddress分配当前状态并在MyAddress上获取

enter image description here

在这里分配:

-(IBAction)adress:(id)sender {

AppDelegate *appDelegate  = (AppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.state = CreateProfile;
MyAddress *my =[[MyAddress alloc]initWithNibName:@"MyAddress" bundle:nil];
[self.navigationController pushViewController:my animated:YES];

}

来到这里:

AppDelegate *appDelegate  = (AppDelegate *)[[UIApplication sharedApplication] delegate];
if (appDelegate.state == EditProfile ) {
    mobile.text =[[NSUserDefaults standardUserDefaults]objectForKey:@"mobile"];
}