在不同的View Controller iOS中调用变量

时间:2014-07-08 03:34:12

标签: ios objective-c

所以我有两个视图控制器回家,第二个。我在home中创建一个变量,并希望在第二个UIViewController中调用它。我收到一个错误说:

使用未声明的标识符'plusNumber'

请告诉我为什么我没有将第二个UIViewController与家中的变量连接起来。我将头文件home.h导入到second.h文件中。

感谢。

代码如下: 的 home.h

@interface homeViewController : UIViewController <UIAlertViewDelegate>
{
    int plusNumber;  
}
- (IBAction)plusSign:(id)sender;

home.m

- (IBAction)plusSign:(id)sender {

    if(self.plusNumber == 0)
    {
        self.plusNumber = @"1";
    } 
    else 
    {
        self.plusNumber = @"0";
    }

}

second.h

#import "home.h"

@property (weak, nonatomic) IBOutlet UILabel *output;

second.m

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.output.text = [NSString stringWithFormat:@"%i", plusNumber];
}

6 个答案:

答案 0 :(得分:1)

您必须将其声明为公共变量

所以你必须将home.h接口更改为

@interfacehomeViewController : UIViewController <UIAlertViewDelegate>
 {
  @public
  int plusNumber;  
 }

更多信息:How to access @public instance variable from another class in Objective-C?

答案 1 :(得分:0)

使用属性。

@interface HomeViewController : UIViewController
@property (nonatomic) int plusNumber;
@end

答案 2 :(得分:0)

在home.m中,从view1到view2创建一个segue并为其提供一个segueIdentifier 在home.h文件中导入second.h

- (IBAction)plusSign:(id)sender {

if(self.plusNumber == 0)
{
    self.plusNumber = @"1";
[self performSegueWithItentifier @"segueName"];
} 
else 
{
    self.plusNumber = @"0";
   [self performSegueWithItentifier @"segueName"];
}

}


-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
  Second secondInstance = [segue destinationViewController];
secondInstance.output = self.plusNumber;

}

在Second.h中删除import语句,否则它将进入内存泄漏周期 创建一个NSString命名 @property(非原子,强)NSString * outputStr;

 viewDidLoad{
   self.output.text = self.outputStr;
   }
瞧,瞧 希望它有所帮助

答案 3 :(得分:0)

<强> home.h

@property (nonatomic) int plusNumber;

<强> home.m

- (IBAction)plusSign:(id)sender {
   [self performSegueWithItentifier @"segue"];
}


-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if([segue.identifier isEqualToString:@"segue"]){
        SecondViewController *controller = 
                         (SecondViewController *)segue.destinationViewController;
        controller.plusNumberString  = (self.plusNumber == 0) ?  @"1" :  @"0";
    }
}

<强> second.h

@property (nonatomic, copy) NSString *plusNumberString;

<强> second.m

-(void)viewDidLoad{
    NSLog(@"_plusNumberString = %@", _plusNumberString);
}

答案 4 :(得分:0)

在Objective C中,编码方式我们称之为getter&amp; amp; setter方法。

您可以通过将属性声明为变量来创建getter和setter。 由于您使用的是int数据类型,因此只需按以下方式分配

@Property(nonatomic, assign) int plusNumber

HomeViewController.h

@interface homeViewController : UIViewController <UIAlertViewDelegate>
{
    int plusNumber;  
}
@Property(nonatomic, assign) int plusNumber
- (IBAction)plusSign:(id)sender;

SecondViewController.m

#import "homeViewController.h"

- (void)viewDidLoad
{
    [super viewDidLoad];
    homeViewController *homecontroller = [[homeViewController alloc] init]
    self.output.text = [NSString stringWithFormat:@"%i", homecontroller.plusNumber];
}

答案 5 :(得分:0)

HomeViewController.h
    @interfacehomeViewController : UIViewController <UIAlertViewDelegate>
     {

      NSUInteger plusNumber;  
     }
     @property (nonatomic,assign) NSUInteger plusNumber;

HomeViewController.m
@synthesize plusNumber;

SecondViewController.m

#import "homeViewController.h"

- (void)viewDidLoad
{
    [super viewDidLoad];
    homeViewController *homecontroller = [[homeViewController alloc] init]
    self.output.text = [NSString stringWithFormat:@"%i", homecontroller.plusNumber];
}

希望它有所帮助。