简体   繁体   English

NSInteger到另一个视图

[英]NSInteger to another View

I need to pass an NSInteger to my Other View Controller: 我需要将NSInteger传递给我的其他视图控制器:

NSInteger index = [carousel indexOfItemView:sender];

This was implemented in a UIButton , Im thinking of using the NSUserDefaults like this sample code here: 这是在UIButton中实现的,我想在这里使用像这个示例代码的NSUserDefaults

[[NSUserDefaults standardUserDefaults] setObject:4 forKey:@"integer"];
[[NSUserDefaults standardUserDefaults] synchronize];

But I can't do it with my code above. 但我不能用上面的代码来做。 Is there other methods. 还有其他方法吗?

You can use NSUserdefaults to pass value from one Viewcontroller to another. 您可以使用NSUserdefaults将值从一个Viewcontroller传递到另一个Viewcontroller。 Use the below code: 使用以下代码:

NSInteger index = [carousel indexOfItemView:sender];
[[NSUserDefaults standardUserDefaults] setInteger:index forKey:@"integer"];
[[NSUserDefaults standardUserDefaults] synchronize];

You can retrieve it on another view by using the following code: 您可以使用以下代码在另一个视图上检索它:

 NSInteger savedIndex = [[NSUserDefaults standardUserDefaults] integerForKey:@"integer"]; 

From the view controller the UIButton is in, you need to get a reference of your other view controller, then have a property on it to set the value. 从UIButton所在的视图控制器中,您需要获取其他视图控制器的引用,然后在其上有一个属性来设置该值。

One way, would be to have a property on the UIViewController that holds the UIButton to assign a reference to the view controller you need to set a value on. 一种方法是在UIViewController上拥有一个属性,该属性包含UIButton以指定您需要设置值的视图控制器的引用。 Set that value when you make the button view controller. 在制作按钮视图控制器时设置该值。

Another way, would be to have a reference to the target view controller in your application delegate, which you can get to from anywhere via the [[UIApplication sharedApplication] delegate] call. 另一种方法是在应用程序委托中引用目标视图控制器,您可以通过[[UIApplication sharedApplication]委托]调用从任何地方获取该控制器。

You may also want to search StackOverflow more, this question I am sure has been answered before... 您可能还想更多地搜索StackOverflow,我相信这个问题之前已经得到了回答......

Use below code: 使用以下代码:

[[NSUserDefaults standardUserDefaults]setInteger:4 forKey:@"integer"];
[[NSUserDefaults standardUserDefaults] synchronize];

Or you can use property to pass value to another view controller if, you are transiting from current view controller to that view controller and you need that value only in next view controller not in entire app.I mean suppose you are in viewController1 and you are transiting from viewController1 to viewController2.And you want index value in viewController2.In this case you can declare property in viewController2.h as: 或者您可以使用属性将值传递给另一个视图控制器,如果您从当前视图控制器转移到该视图控制器,并且您只需要在下一个视图控制器中而不是整个app中使用该值。我的意思是假设您在viewController1中并且您是从viewController1转换到viewController2.And你想要viewController2中的索引值。在这种情况下,你可以在viewController2.h中声明属性:

@property(nonatomic,assign)int indexInViewController2;

synthesize in viewController2.m as follows:

@synthesize indexInViewController2;

and while transiting from viewController1 to viewController2 you can pass value as follows: 从viewController1转换到viewController2时,您可以传递值,如下所示:

viewcontroller2Obj.indexInViewController2 = [carousel indexOfItemView:sender];

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

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