-1

我正在尝试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
4

1 回答 1

1

不太清楚你是如何推动你的视图控制器的,但这里是你如何从一个视图控制器捕获值到另一个。

如果您使用的是故事板,则可以prepareForSegue:sender:在视图控制器 1 中使用:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString @"some string"])
    {
        SecondViewController *vc = [segue destinationViewcontroller];
        [[vc label] setText:@"SomeText"];
    }
}

如果您不使用情节提要,您可以在展示或推送您的第二个视图控制器之前调用您的方法:

- (void)someMethod
{
    SecondViewController *vc = [[SecondViewController alloc] initWithNibName:@"somexib" bundle:nil];
    [[vc label] setText:@"sometext"];
    [self presentViewController:vc animated:yes];
}

这应该让您了解如何使用情节提要和使用常规视图控制器推送将值从一个控制器传递到另一个控制器。

于 2013-09-04T21:03:42.320 回答