【问题标题】:how to delegate with an IBAction between two different UIViewController如何在两个不同的 UIViewController 之间使用 IBAction 进行委托
【发布时间】:2013-03-09 02:05:48
【问题描述】:

我只是想了解委托的工作原理,但我遇到了麻烦。

我有两个类(都是 UIViewController)连接到情节提要中,第一个(ViewController.h/m)保存一个带有单元格的 TableView,第二个(AddNameViewController.h/m)只保存一个 TextField(我想要写)和一个按钮(添加名称)

您肯定明白,我希望按下按钮将写入 TextField 的内容发送到 TableView,非常简单。

由于我有两个不同的控制器和一个包含 tableview 保存的数据的数组,我想将它们与一个委托连接(只是为了学习它)。

这里有一些代码:

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

数组已正确初始化,没有错误,没有警告,没有崩溃,只是看起来甚至没有调用方法“addStringWithString”,因为它甚至不是 NSLog 任何东西。

很明显,故事板、方法和出口中的所有内容都已连接,感谢您的帮助。

【问题讨论】:

    标签: objective-c delegates protocols ibaction delegation


    【解决方案1】:

    在 AddNameViewController 的界面构建器中,您是否将按钮事件(Touch Up inside)连接到动作 -(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"];
    }
    

    【讨论】:

    • 是的,按钮事件是连接到“touch up inside”进入情节提要的正确方法(IBAction)add:(id)sender,显然进入了“AddNameViewController”,我已经也尝试使用您提到的“if 语句”,但没有任何反应
    • 我在你所做的 if 语句中添加了一个 NSLog,但没有打印任何内容,所以委托没有响应该选择器,不知道为什么......
    • 首先启动哪个视图控制器,ViewcOntroller 或 AddNameViewController,因为如果 AddNameViewController 是第一个,它的委托永远不会初始化,NSLog(self.delegate) 为 nil
    • ViewController 是第一个,如果我添加 [anvc nameMethod];在 ViewController 的 viewDiDLoad 中调用了该方法,因此初始化了委托,但我想通过触摸按钮来调用该方法,而不是在 viewDidLoad
    • 似乎委托已初始化并随后被释放,这就是为什么如果我添加 [anvc method];进入 viewdidload 它可以工作,否则不会,我不知道为什么会被释放..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多