Ios 从其他视图控制器访问实例变量

Ios 从其他视图控制器访问实例变量,ios,objective-c,Ios,Objective C,我在mainViewController.m中有一个公开声明的NSString,NSString*characterString,就在我执行[self-performsguewithidentifier:@“seguetoffal”发送方:self]之前I使用最新数据更新此characterString。像这样: characterString=[NSString stringWithFormat:@"final string %@",truncatedString]; [self perfor

我在mainViewController.m中有一个公开声明的NSString,
NSString*characterString
,就在我执行
[self-performsguewithidentifier:@“seguetoffal”发送方:self]之前
I使用最新数据更新此
characterString
。像这样:

characterString=[NSString stringWithFormat:@"final string %@",truncatedString];
[self performSegueWithIdentifier:@"segueToBlueprints" sender:self];
当新的视图控制器完成显示时,用户将在某个时间按下一个按钮,该按钮将调用一个方法,该方法将需要
characterString
,并且可能需要来自先前视图控制器的其他公开声明的实例变量。我尝试了
[[mainViewController alloc]getCharacterString]
(在mainViewController中实现了
getCharacterString
方法),但这当然会创建一个mainViewController的新实例,并且不能解决问题


如何从旧视图控制器访问“characterString”中当前的数据和其他变量

将所有全局变量保存在AppDelegate中:

YourAppDelegate.h

@interface BTAppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@property (strong, nonatomic) NSString * characterString;

@end
YourSecondViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];
    YourAppDelegate* app = (YourAppDelegate*)[UIApplication sharedApplication].delegate;

    app.characterString = @"Hello"; 

}
- (IBAction)pressButton:(id)sender{
YourAppDelegate* app = (YourAppDelegate*)[UIApplication sharedApplication].delegate;

        app.characterString = @"Hello2"; 


}

等等。

将所有全局变量保存在AppDelegate中:

YourAppDelegate.h

@interface BTAppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@property (strong, nonatomic) NSString * characterString;

@end
YourSecondViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];
    YourAppDelegate* app = (YourAppDelegate*)[UIApplication sharedApplication].delegate;

    app.characterString = @"Hello"; 

}
- (IBAction)pressButton:(id)sender{
YourAppDelegate* app = (YourAppDelegate*)[UIApplication sharedApplication].delegate;

        app.characterString = @"Hello2"; 


}

等等。

我能想到的唯一一件事就是将旧数据与segue一起从视图控制器传递出去。或者,如果要保留多个属性,甚至可以传递整个旧视图控制器

在新VC(被分割到的VC)中,添加一个要从旧VC(被分割到的VC)保留的属性,例如
NSString*

@interface NewVC :UIViewController
@property (nonatomic) NSString *newCharacterString;
@end
然后,当您继续时,只需将值传递给新的VC

characterString=[NSString stringWithFormat:@"final string %@",truncatedString];

newViewControllerIWantToPassAValueTo.newCharacterString = characterString; 
// passes new string forward to View Controller

[self performSegueWithIdentifier:@"segueToBlueprints" sender:self];

我能想到的唯一一件事就是将旧数据与segue一起从视图控制器传递出去。或者,如果要保留多个属性,甚至可以传递整个旧视图控制器

在新VC(被分割到的VC)中,添加一个要从旧VC(被分割到的VC)保留的属性,例如
NSString*

@interface NewVC :UIViewController
@property (nonatomic) NSString *newCharacterString;
@end
然后,当您继续时,只需将值传递给新的VC

characterString=[NSString stringWithFormat:@"final string %@",truncatedString];

newViewControllerIWantToPassAValueTo.newCharacterString = characterString; 
// passes new string forward to View Controller

[self performSegueWithIdentifier:@"segueToBlueprints" sender:self];
试试这个:

@interface NewVC :UIViewController
@property (nonatomic) NSString *newCharacterString;
@end
现在,在mainViewController中导入NewViewController类:

#import "NewViewController.h"
.......
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    NewViewController *destinationViewController = [segue destinationViewController];
    destinationViewController.newCharacterString = self.characterString;
}
您可以通过newCharacterString属性访问NewViewController中字符串的值。 HTH:)

试试这个:

@interface NewVC :UIViewController
@property (nonatomic) NSString *newCharacterString;
@end
现在,在mainViewController中导入NewViewController类:

#import "NewViewController.h"
.......
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    NewViewController *destinationViewController = [segue destinationViewController];
    destinationViewController.newCharacterString = self.characterString;
}
您可以通过newCharacterString属性访问NewViewController中字符串的值。
HTH:)

为什么不将此字符串传递给secondViewController

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Make sure your segue name in storyboard is the same as this line
    if ([[segue identifier] isEqualToString:@"YOUR_SEGUE_NAME_HERE"])
    {
        SecondViewController *vc = [segue destinationViewController];

        // Pass any objects to the view controller here, like...
        vc.newCharacterString = self.characterString;
    }
}
在SecondViewController中:

@interface SecondViewController:UIViewController
@property (strong, nonatomic) NSString *newCharacterString;
@end
[self performSegueWithIdentifier:@"segueToBlueprints" sender:self];
在MainViewController中:

@interface SecondViewController:UIViewController
@property (strong, nonatomic) NSString *newCharacterString;
@end
[self performSegueWithIdentifier:@"segueToBlueprints" sender:self];
您必须将preparePerform序列添加到MainViewController

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Make sure your segue name in storyboard is the same as this line
    if ([[segue identifier] isEqualToString:@"YOUR_SEGUE_NAME_HERE"])
    {
        SecondViewController *vc = [segue destinationViewController];

        // Pass any objects to the view controller here, like...
        vc.newCharacterString = self.characterString;
    }
}

您可以在SecondViewController中使用newCharacterString,而无需回调MainViewController

为什么不将此字符串传递给SecondViewController

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Make sure your segue name in storyboard is the same as this line
    if ([[segue identifier] isEqualToString:@"YOUR_SEGUE_NAME_HERE"])
    {
        SecondViewController *vc = [segue destinationViewController];

        // Pass any objects to the view controller here, like...
        vc.newCharacterString = self.characterString;
    }
}
在SecondViewController中:

@interface SecondViewController:UIViewController
@property (strong, nonatomic) NSString *newCharacterString;
@end
[self performSegueWithIdentifier:@"segueToBlueprints" sender:self];
在MainViewController中:

@interface SecondViewController:UIViewController
@property (strong, nonatomic) NSString *newCharacterString;
@end
[self performSegueWithIdentifier:@"segueToBlueprints" sender:self];
您必须将preparePerform序列添加到MainViewController

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Make sure your segue name in storyboard is the same as this line
    if ([[segue identifier] isEqualToString:@"YOUR_SEGUE_NAME_HERE"])
    {
        SecondViewController *vc = [segue destinationViewController];

        // Pass any objects to the view controller here, like...
        vc.newCharacterString = self.characterString;
    }
}

您可以在SecondViewController中使用newCharacterString,而无需回调MainViewController

OK。我可以为
characterString
和其他一个或两个数据创建
NSString
类文件吗?我能以任何方式从中获取数据吗?好的。我可以为
characterString
和其他一个或两个数据创建
NSString
类文件吗?我能以任何方式从中获取数据吗?好的。我可以为
characterString
和其他一个或两个数据创建
NSString
类文件吗?“我能以任何方式从中获取我的数据吗?”我不知道这有什么问题。也许是因为全局变量是个坏主意。至少留下评论+1安全!应用程序委托中的所有变量将一直有效,直到您的应用程序生效。谢谢Sergey,这太棒了。@SergeyNeskoromny是否有办法访问视图控制器中存在的实例变量,该视图控制器在自身上呈现另一个视图控制器?确定。我可以为
characterString
和其他一个或两个数据创建
NSString
类文件吗?“我能以任何方式从中获取我的数据吗?”我不知道这有什么问题。也许是因为全局变量是个坏主意。至少留下评论+1安全!应用程序委托中的所有变量将一直有效,直到您的应用程序有效。谢谢Sergey,这太棒了。@SergeyNeskoromny是否有办法访问视图控制器中的实例变量,该视图控制器在自身上呈现另一个视图控制器?
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Make sure your segue name in storyboard is the same as this line
    if ([[segue identifier] isEqualToString:@"YOUR_SEGUE_NAME_HERE"])
    {
        SecondViewController *vc = [segue destinationViewController];

        // Pass any objects to the view controller here, like...
        vc.newCharacterString = self.characterString;
    }
}