【问题标题】:How to call the Delegate from viewcontroller to tableview如何从 viewcontroller 调用 Delegate 到 tableview
【发布时间】:2012-08-22 21:43:50
【问题描述】:

我将 TableView 作为我的 MainView 并将 viewcontroller 作为我的第二个视图。视图控制器有 3 个文本字段..我正在编写协议,以便我可以获得 Textfileds 的值并可以添加到 Tableview 数组中。目的是我在文本字段中给出的任何内容都必须添加到表格单元格中。 我有 secondviewcontroller.h

@property (nonatomic,assign) id<secondviewcontrollerDelegate> delegate;

 @protocol secondviewcontrollerDelegate<NSObject>
- (void)additemsintoarray;
@end

secondviewcontroller.m

-(IBAction)ok:(id)sender
{
    [self.delegate additemsintoarray];
}

在 Tableview.h 中

@interface RootViewController : UITableViewController<secondviewcontrollerDelegate> 

在tableview.m中

- (void)additemsintoarray
{
    [self.array1 insertObject:scontroller.iditem1.text atIndex:[array1 count]];
    [self.array1 insertObject:scontroller.nameitem1.text atIndex:[array1 count]];
    [self.array1 insertObject:scontroller.iditem1.text atIndex:[array1 count]];
    [[self navigationController]popViewControllerAnimated:YES];
}

注意:数组在 Tableview 中声明了一些值....
我收到一个错误:

由于未捕获的异常 'NSInvalidArgumentException' 导致应用程序终止,原因:'*** -[UIView additemsintoarray:]:无法识别的选择器已发送到实例

【问题讨论】:

  • 让我们看看你在哪里设置了委托。 (另外,你能格式化你的代码吗?用纯文本很难理解。)
  • 我在 SecondViewController 中设置了 Delegate(即我的 Viewcontroller)
  • 无论您设置为什么委托,要么是UIView,而不是您认为的对象您的委托对象已被释放并且其内存被重新使用对于UIView。请显示设置委托的代码。
  • 我无法得到你.. 我将委托人连接到 UIVIEW 的 IB
  • 我使用了上面的代码.. 除了使用委托之外我没有做任何事情

标签: objective-c ios cocoa-touch uiviewcontroller delegates


【解决方案1】:

您的RootViewController 应该是代表。您遇到此问题是因为 UIView 作为您的 SecondViewController 的代表连接。

希望对你有帮助

【讨论】:

  • 如何将secondviewcontroller的delegate设置为Rootviewcontroller
  • 对不起,我是代理概念的新手。你能帮帮我吗?
  • 我需要如何设置委托来获得结果?
  • 在您的 RootViewController 中,如果您有对 SecondViewController 的引用(例如,称之为 _secondVC)。只需将您的 SecondViewController 设置如下:_secondVC.delegate = self;
  • 我之前试过这个但是把代码放错了地方..你清除了它..你只是摇摆!!!!继续做:-)
猜你喜欢
  • 2014-09-16
  • 2011-07-02
  • 1970-01-01
  • 2016-05-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-06
  • 1970-01-01
相关资源
最近更新 更多