我正在尝试UITextField
在我的第一个视图控制器中输入一些文本,并且我希望我输入的文本在另一个集合视图控制器中显示为标签,就像 Google keep 或任何一般的笔记应用程序一样。我很难让文本显示在我的第二个集合视图控制器单元格中。
第一个视图控制器标题:
@interface ViewController : UIViewController
<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField *ideaText;
@property (weak, nonatomic) IBOutlet UIButton *enterButton;
第二个集合视图控制器头:
#import <UIKit/UIKit.h>
#import "ViewController.h"
@interface IDEERViewController : UICollectionViewController
<UICollectionViewDataSource, UICollectionViewDelegate>
@property (strong, nonatomic) IBOutlet UICollectionView *iDEERCollectionView;
@property (nonatomic, retain) ViewController *previousViewController;
集合视图控制器实现:
#import "IDEERViewController.h"
#import "ViewController.h"
#import "CollectionViewCell1.h"
@interface IDEERViewController (){
NSArray *ideerLabel;
}
@end
@implementation IDEERViewController
@synthesize iDEERCollectionView;
@synthesize previousViewController;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[self.iDEERCollectionView setDelegate:self];
[self.iDEERCollectionView setDataSource:self];
// Initialize recipe image array
NSString *textValue = [NSString stringWithFormat:@"%@",previousViewController.ideaText.text];
ideerLabel = [NSArray arrayWithObjects:textValue, nil];
//
// self.collectionLabel.text = @"hello";
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return ideerLabel.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = @"Cell";
CollectionViewCell1 *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
[[cell collectionLable]setText:[ideerLabel objectAtIndex:indexPath.item]];
return cell;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
现在,在我运行程序并转到集合视图页面之后,我得到(Null)
的不是我在第一个控制器的文本字段中键入的任何内容。
非常感谢你的帮助!
编辑 这里是第一个控制器的来源
#import "ViewController.h"
#import "AppDelegate.h"
@interface ViewController ()
@end
@implementation ViewController{
}
@synthesize ideaText;
@synthesize enterButton;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(IBAction)onDismissKeyboard:(id)sender {
[ideaText resignFirstResponder];
}
-(void) handleBackgroundTap:(UITapGestureRecognizer *)sender
{
[ideaText resignFirstResponder];
}
//press enter and send text to next view.
@end