- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.item > 0 )
{
myNumber = indexPath.item;
NSLog(@"Item is %i", myNumber);
NSString *myNewString = [@(myNumber) stringValue];
[txt setText:myNewString];
self.datastring=txt.text;
WeddingViewController *wed=[[WeddingViewController alloc]init];
[self presentViewController:wed animated:YES completion:nil];
}
}
并在第二个集合视图的viewDidLoad
中.....
- (void)viewDidLoad
{
[super viewDidLoad];
self.lbl.text =self.datastring;
}
答案 0 :(得分:0)
你可以像这样为WeddingViewController创建一个自定义构造函数(init)方法以及一个保存该值的属性:
在WeddingViewController.h
文件中,创建构造函数方法:
@property (nonatomic, assign) int storedIntValue;
-(id)initWithValue:(int)intValue;
然后在您的实施文件中,您可以:
-(id)initWithValue:(int)intValue
{
self = [super init];
if(self)
{
self.storedIntValue = intValue;
}
return self;
}
-(void)viewDidLoad
{
....
self.lbl.text = [[NSString alloc] initWithFormat:@"%d", self.storedIntValue];
}
最后,在你推送WeddingViewController的地方,你可以去:
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.item > 0 )
{
myNumber = indexPath.item;
NSLog(@"Item is %i", myNumber);
NSString *myNewString = [@(myNumber) stringValue];
[txt setText:myNewString];
self.datastring=txt.text;
// ----------------------------------------------------
// Notice use of the "initWithIntValue:" method
//
// note: is myNumber meant to be indexPath row ?
// ----------------------------------------------------
WeddingViewController *wed=[[WeddingViewController alloc] initWithValue:myNumber];
[self presentViewController:wed animated:YES completion:nil];
}
}
否则,替代方法是在应用程序的某个地方存储可以全局访问的int值,例如单例,或者如果它是用户默认值,则可以使用:
// save value into NSUserDefaults if it's appropriate
[[NSUserDefaults standardDefaults] setValue:[NSNumber numberWithInt:3] forKey:@"myIntValue"];
[NSUserDefaults synchronize];
// get value from NSUserDefaults
int myIntValue = [[[NSUserDefaults standardDefaults] valueForKey:@"myIntValue"] integerValue];
这有帮助吗?
答案 1 :(得分:0)
你必须这样做,你可以在WeddingViewController.h中创建字符串属性并直接在viewDidLoad中对其进行处理,
@property (nonatomic, assign) NSString dataString;
并在viewWillAppear
中 -(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
self.lbl.text = dataString;
}
and when make WeddingViewController object give property like this,
WeddingViewController *wed=[[WeddingViewController alloc]init];
wed.dataString = dataString;
[self presentViewController:wed animated:YES completion:nil];