Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 在UIAbbarController xcode中的UIViewController和UIViewController之间共享数据_Iphone_Xcode - Fatal编程技术网

Iphone 在UIAbbarController xcode中的UIViewController和UIViewController之间共享数据

Iphone 在UIAbbarController xcode中的UIViewController和UIViewController之间共享数据,iphone,xcode,Iphone,Xcode,我知道如何在两个视图之间共享数据。但如果我想使用tabBarController共享数据,我就迷路了 这是我移动到选项卡栏的操作 -(IBAction)goToPage2:(id)sender { tabController.modalTransitionStyle=UIModalTransitionStyleFlipHorizontal; [self presentModalViewController:tabController animated:YES]; } firstView *

我知道如何在两个视图之间共享数据。但如果我想使用tabBarController共享数据,我就迷路了

这是我移动到选项卡栏的操作

-(IBAction)goToPage2:(id)sender
{
 tabController.modalTransitionStyle=UIModalTransitionStyleFlipHorizontal;
 [self presentModalViewController:tabController animated:YES];
}
firstView *first = [[firstView alloc] initWithNibName:@"firstView" bundle:nil];
first.dataStr = name.text;
[tabController  presentModalViewController:first animated:YES];
我需要在选项卡栏的第一个视图中的IBAction中共享我的NSString*dataStr

-(IBAction)goToPage2:(id)sender
{
 tabController.modalTransitionStyle=UIModalTransitionStyleFlipHorizontal;
 [self presentModalViewController:tabController animated:YES];
}
firstView *first = [[firstView alloc] initWithNibName:@"firstView" bundle:nil];
first.dataStr = name.text;
[tabController  presentModalViewController:first animated:YES];
这个代码不起作用


thx

在应用程序委托中声明@属性。您可以从应用程序的任何点访问应用程序代理

MyAppDelegate *appDelegate =  (MyAppDelegate *)[[UIApplication sharedApplication ]delegate]

在应用程序委托中声明@属性。您可以从应用程序的任何点访问应用程序代理

MyAppDelegate *appDelegate =  (MyAppDelegate *)[[UIApplication sharedApplication ]delegate]

我在博客上写了一篇关于这些问题的长篇教程:


您需要找到一种干净的方式让控制器彼此通信。我的教程介绍了几种方法,以及每种方法的优缺点。学习如何做到这一点是值得的,因为这个问题几乎出现在你将要编写的任何应用程序中。

我在我的博客上写了一篇关于此类问题的冗长教程:


您需要找到一种干净的方式让控制器彼此通信。我的教程介绍了几种方法,以及每种方法的优缺点。这是值得学习的,因为这个问题几乎出现在你将要编写的任何应用程序中。

我同意特伦特的建议。但我建议您在这种情况下使用数据类。在Appdelegate中使用属性不是一种好做法。您应该始终为此使用数据类。
您可以按如下方式创建数据类:
您需要创建一个数据类,可以在其中设置变量或案例数组的属性(用于在UITableView中显示数据)。在数据类中实现一个类方法,用于检查对象是否已实例化。如果没有,它就会这样做。是这样的:

//数据类.h

@interface DataClass : NSObject {  

NSMutableArray *nameArray;  
NSMutableArray *placeArray;     

}  
@property(nonatomic,retain)NSMutableArray *nameArray;  
@property(nonatomic,retain)NSMutableArray *placeArray;  
+(DataClass*)getInstance;  
@end  
//DataClass.m

@implementation DataClass  
@synthesize nameArray;  
@synthesize placeArray;  
static DataClass *instance =nil;  
+(DataClass *)getInstance  
{  
    @synchronized(self)  
    {  
        if(instance==nil)  
        {  

            instance= [DataClass new];  
        }  
    }  
    return instance;  
}  
现在,在视图控制器中,您需要调用以下方法:

DataClass *obj=[DataClass getInstance];  
并使用阵列。

通过这种方式,您可以在不干扰AppDelegate的情况下分配数据,这是一种很好的做法。

我同意特伦特的建议。但我建议您在这种情况下使用数据类。在Appdelegate中使用属性不是一种好做法。您应该始终为此使用数据类。
您可以按如下方式创建数据类:
您需要创建一个数据类,可以在其中设置变量或案例数组的属性(用于在UITableView中显示数据)。在数据类中实现一个类方法,用于检查对象是否已实例化。如果没有,它就会这样做。是这样的:

//数据类.h

@interface DataClass : NSObject {  

NSMutableArray *nameArray;  
NSMutableArray *placeArray;     

}  
@property(nonatomic,retain)NSMutableArray *nameArray;  
@property(nonatomic,retain)NSMutableArray *placeArray;  
+(DataClass*)getInstance;  
@end  
//DataClass.m

@implementation DataClass  
@synthesize nameArray;  
@synthesize placeArray;  
static DataClass *instance =nil;  
+(DataClass *)getInstance  
{  
    @synchronized(self)  
    {  
        if(instance==nil)  
        {  

            instance= [DataClass new];  
        }  
    }  
    return instance;  
}  
现在,在视图控制器中,您需要调用以下方法:

DataClass *obj=[DataClass getInstance];  
并使用阵列。

通过这种方式,您可以在不干扰AppDelegate的情况下分配数据,这是一种很好的做法。

当然可以。如果您发现答案正确,请投票并接受:)@Nitish我正试图使用您的示例创建一个数据类,以便在MainViewController和SecondViewController(碰巧是表视图)之间共享日历事件数组。为了使用
DataClass*obj=[DataClass getInstance]访问数组,我应该如何“设置”数组本身?是的,当然。如果您发现答案正确,请投票并接受:)@Nitish我正试图使用您的示例创建一个数据类,以便在MainViewController和SecondViewController(碰巧是表视图)之间共享日历事件数组。为了使用
DataClass*obj=[DataClass getInstance]访问数组,我应该如何“设置”数组本身