1

デリゲートがどのように機能するかを理解しようとしているだけで、問題が発生しています。

ストーリーボードに接続された2つのクラス(両方ともUIViewController)があります。最初のクラス(ViewController.h / m)はセルを含むTableViewを保持し、2番目のクラス(AddNameViewController.h / m)は単にTextField(書き込みたい場所)を保持しますとボタン(名前の追加)

確かに理解しているように、ボタンを押すと、TextFieldに書き込まれた内容がTableViewに送信されます。これは非常に簡単です。

また、2つの異なるコントローラーとテーブルビューによって保持されているデータを含む配列があるので、それらをデリゲートに接続します(それを学ぶためだけに)。

ここにいくつかのコードがあります:

ViewController.h

#import "AddNameViewController.h"
@interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource, AddNameViewControllerDelegate>
@property (strong, nonatomic) NSMutableArray *array;
@end

ViewController.m

#import "ViewController.h"
#import "AddNameViewController.h"
@inferface ViewController ()

@end

@implementation ViewController
@synthesize array;

-(void)addStringWithString:(NSString*)string
{
[self.array addObject:string];
NSLog(@"%@", array);
}

-(void)viewDidLoad
{
AddNameViewController *anvc = [[AddNameViewController alloc] init];
anvc.delegate = self;

array = [[NSMutableArray alloc] initWithObjects:@"first", @"second", nil];
NSLog(@"%@", array);
[super viewDidLoad];

}

-(NSInteger)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSindexPath*)indexPath
{
static NSString *simpleTableIdentifier = @"RecipeCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}

cell.textLabel.text = [array objectAtIndex:indexPath.row];
return cell;
}

@end

AddNameViewController.h

@protocol AddNameViewControllerDelegate <NSObject>

-(void)addStringWithString:(NSString*)string;

@end

@interface AddNameViewController : UIViewController

@property (weak, nonatomic) id <AddNameViewControllerDelegate> delegate;
@property (weak, nonatomic) IBOutlet UITextField *myTextField;

-(IBAction)add:(id)sender;

@end

最後にAddNameViewController.m

#import "ViewController.h"

@interface AddNameViewController ()

@end

@implementation AddNameViewController
@synthesize myTextField, delegate;

-(id)initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)  {
}
return self;
}

-(void)viewDidLoad
{
[super viewDidLoad];
}

-(IBAction)add:(id)sender
{
[self.delegate addStringWithString:self.myTextField.text];
// I've also tried with this but nothing --> [self.delegate addStringWithString:@"aa"];
}

@end

配列は適切に初期化され、エラー、警告、クラッシュは発生しません。NSLogも何もないため、メソッド「addStringWithString」も呼び出されていないように見えます。

ストーリーボード、メソッド、アウトレットですべてが接続されていることは明らかです。ご協力いただきありがとうございます。

4

1 に答える 1

0

AddNameViewControllerのインターフェイスビルダーで、ボタンイベント(内側のタッチアップ)をアクション-(IBAction)add:(id)senderに接続しましたか?

これも試してみてください

-(IBAction)add:(id)sender
{
 if([self.delegate respondsToSelector:@selector(addStringWithString:)]) {
[self.delegate addStringWithString:self.myTextField.text];
}
// I've also tried with this but nothing --> [self.delegate addStringWithString:@"aa"];
}
于 2013-03-20T06:22:44.907 に答える