如何从UITextField的第二个类传递值到iPhone的UITableViewCell的第一个视图类



嗨,我尝试了另一个传递数据的代码,但它不工作。如何将UITextField值传递到行上UITableViewCell的第一视图上?谁来帮我一下委托协议

//
//  first.h
//  TextviewExample
//
//  Created by pradeep.yadav on 12/5/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "second.h"
#import "TextviewExampleAppDelegate.h"
//@class second;

@interface first : UITableViewController <secondDelegate>{
    //TextviewExampleAppDelegate*app;
    //TextviewExampleAppDelegate *check;
    second *secondview;
    NSString                *secondFavoriteColorString;
    //second *value;
}
@property (nonatomic, retain) second *secondview;
@property (copy) NSString   *secondFavoriteColorString;
@end


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    // Configure the cell...
    cell.textLabel.text=@"message";
    cell.detailTextLabel.text=secondFavoriteColorString;
    NSLog(@"this second check:%@",secondFavoriteColorString);
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    second *viewTwo = [[second alloc] initWithNibName:@"second" bundle:[NSBundle mainBundle]];
    self.secondview = viewTwo;
    [viewTwo release];
    [self.navigationController pushViewController:self.secondview animated:YES];
}

-(void)setsecond:(NSString*)secondtextview
{
    secondFavoriteColorString = secondtextview;
    NSLog(@"this second check:%@",secondFavoriteColorString);
}

this second class
//
//  second.h
//  TextviewExample
//
//  Created by pradeep.yadav on 12/5/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import <UIKit/UIKit.h>
@protocol secondDelegate<NSObject>
@required
-(void)setsecond:(NSString*)secondtextview ;//forIndexPath:(NSIndexPath*)indexPath;

@end

@interface second : UIViewController {
    IBOutlet UITextField *secondtextfield;
    NSString             *favoriteColorString;
id <secondDelegate> delegate;
}
@property (nonatomic,retain)UITextField *secondtextfield;
//@property (nonatomic,assign)id<secondDelegate>delegate;
@property (retain) id delegate;
@property (nonatomic,copy)NSString           *favoriteColorString;
@end

#import "second.h"

@implementation second
@synthesize delegate,secondtextfield,favoriteColorString;

- (void) viewWillDisappear:(BOOL) animated
{
    [[self delegate] setsecond:secondtextfield.text];
    favoriteColorString=secondtextfield.text;
    NSLog(@"thuis check:%@",favoriteColorString);
}

- (BOOL) textFieldShouldReturn: (UITextField *) theTextField
{
    [theTextField resignFirstResponder];
    return YES;
}

我认为问题是你在第二个视图中声明了委托,而你试图从第一个视图调用它。当您返回到第一个视图时,很可能第二个视图已经被清理干净了。

你要做的是在第一个视图控制器中声明委托方法,当你创建第二个视图控制器时,你将第一个视图控制器设置为第二个视图控制器的委托。

我之前创建了一个示例项目。它不处理表格视图,但它演示了将字符串从第二个视图控制器传递回第一个视图控制器。

这是一个关于如何在viewController之间共享数据的教程的链接:

http://www.iphonedevsdk.com/forum/iphone-sdk-development/54859-sharing-data-between-view-controllers-other-objects.html

它应该能帮助你解决你的问题。

从View1中的TextField到View2中的Cell

在第二视图中取一个字符串。属性,合成它,当从第一个视图移动到第二个视图时,在分配和在按压之前将的值分配给UITextField的合成字符串右。在第二个视图中,在单元格上以合成字符串的形式显示文本。

从View2中的TextField到View1中的Cell

在First View中取字符串。属性,合成它,在secondView和property中取FirstView的一个对象并合成它

从第一个视图移动到第二个视图时,在分配和压入之前给objSecondView.objFirstView=self; 赋值。现在在第二个视图中,将text的值赋给firstViews合成字符串。在firstView的viewWillAppear中将字符串的值赋给单元格

。上面示例中的方法必须与

下面的方法类似。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
 second *viewTwo = [[second alloc] initWithNibName:@"second" bundle:[NSBundle mainBundle]];
 viewTwo.objViewOne = self;
 [self.navigationController pushViewController:viewTwo animated:YES];
 [viewTwo release];
}

这它. .

最新更新