简体   繁体   English

如何在 UINavigationcontroll 上将值从 view2 返回到 view1

[英]How to return values from view2 to view1 on UINavigationcontroll

I am New to the iPhone Development.我是 iPhone 开发的新手。 How can i carry a string value from view2 to view1 when using navigation bar.使用导航栏时,如何将字符串值从 view2 传送到 view1。

I have no problem in carrying string values from view1 to view2 to....by using pushviewcontroller But when i come back to previous views using Back button of navigation bar, I cannot able to hold string values.我可以通过使用 pushviewcontroller 从 view1 到 view2 携带字符串值到....但是当我使用导航栏的后退按钮返回到以前的视图时,我无法保存字符串值。

I already seen post related this "http://stackoverflow.com/questions/2903967/how-return-a-value-from-view2-to-view1-when-using-navigation-bar" and that is not worked for me or may be i did wrong.我已经看到与此“http://stackoverflow.com/questions/2903967/how-return-a-value-from-view2-to-view1-when-using-navigation-bar”相关的帖子,这对我不起作用或者可能是我做错了。

I need your help in solving this issue.我需要你的帮助来解决这个问题。

Thanks in advance,提前致谢,

Nagarajan Govindarajan.纳加拉詹·戈文达拉詹。

You could use a delegate.您可以使用委托。 You could also have a property in view1 that view2 can access and store the string into.您还可以在 view1 中有一个属性, view2 可以访问该字符串并将其存储到其中。 The delegate is the better way to do it.委托是更好的方法。

Take a look at Apple's samples to see how they use delegates.查看 Apple 的示例,了解它们如何使用委托。 The question you refer to is correct, so I think you just need to understand it well enough to be able to debug your implementation.你提到的问题是正确的,所以我认为你只需要充分理解它就可以调试你的实现。

In Your project app delegate class declare and define a string(exp str1).在您的项目应用程序委托 class 中声明并定义一个字符串(exp str1)。 Also alloc and initialize this string.同时分配和初始化这个字符串。

In your view 2 class inport Appdelegate class.在您看来 2 class inport Appdelegate class。 Declare its object like:像这样声明它的 object:

TestAppDelegate *appDeleg;

In viewDidLoad of class 2 define: appDeleg = [[UIApplication sharedApplication] delegate];在 class 2 的 viewDidLoad 中定义: appDeleg = [[UIApplication sharedApplication] delegate];

now store appDeleg.str1 = your string in view2 which you want to store and use in view 1.现在存储 appDeleg.str1 = 您要在视图 1 中存储和使用的视图 2 中的字符串。

do same declaration in view 1 and use there strView1= appDeleg.str1;在视图 1 中做同样的声明并在那里使用 strView1= appDeleg.str1;

The best way I recommend is to use a Data class.我推荐的最佳方式是使用数据 class。 Declare variables in a Data class and use them throughout.在数据 class 中声明变量并在整个过程中使用它们。 This is how it works:这是它的工作原理:

//DataClass.h //数据类.h

@interface DataClass : NSObject {    

NSString *str;     

}    
@property(nonatomic,retain)NSString *str;    
+(DataClass*)getInstance;    
@end  

//DataClass.m //数据类.m

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

             instance= [DataClass new];    
        }    
    }    
    return instance;    
}    

Now in your view controller you need to call this method as:现在在您看来 controller 您需要将此方法称为:

DataClass *obj=[DataClass getInstance];  
obj.str= @"I am Global variable";  

This variable will be accessible to every view controller.每个视图 controller 都可以访问此变量。 You just have to create an instance of Data class.您只需创建数据 class 的实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM