在另一个viewcontroller中设置属性

时间:2014-02-28 13:39:53

标签: ios objective-c uiviewcontroller

您好我试图在masterviewcontoller中设置一个属于另一个viewcontroller的属性,但它不会进入set方法。我用Masterviewcontroller对Detailviewcontroller做了这个,它运行正常。现在我复制该代码并尝试使用masterviewcontroller对名为AddNoteViewController的控制器执行相同操作。相同的代码,但它不会进入set方法。 我有两个类相同的importet 谢谢你的帮助!

在调试中有效

第1步masterviewController.m

//MasterViewController.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSDate *object = _objects[indexPath.row];
NSDate *kundnr = _kundnr[indexPath.row];
self.detailViewController.detailItem = object;
self.detailViewController.detailIdentity = kundnr;
self.addNoteViewController.AddNoteFtgnr = kundnr;

}

Step2 DetailViewController.h

//DetailViewController.h
@property (strong, nonatomic) id detailItem;
@property (strong, nonatomic) id detailIdentity;

第3步DetailViewController.m

//DetailViewController.m
- (void)setDetailItem:(id)newDetailItem
{
if (_detailItem != newDetailItem) {
    _detailItem = newDetailItem;

    // Update the view.
    [self configureView];
}

if (self.masterPopoverController != nil) {
    [self.masterPopoverController dismissPopoverAnimated:YES];
}        
}

- (void)setDetailIdentity:(id)newDetailItentity
{
if (_detailIdentity != newDetailItentity) {
    _detailIdentity = newDetailItentity;

    // Update the view.
    [self configureView];
}

if (self.masterPopoverController != nil) {
    [self.masterPopoverController dismissPopoverAnimated:YES];
}
}

现在无效的AddNoteviewController

第1步masterviewController.m (我认为这是好的)

//MasterViewController.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSDate *object = _objects[indexPath.row];
NSDate *kundnr = _kundnr[indexPath.row];
self.detailViewController.detailItem = object;
self.detailViewController.detailIdentity = kundnr;
self.addNoteViewController.AddNoteFtgnr = kundnr;

}

第2步AddNoteViewController.h (我认为这是好的)

//AddNoteViewController.h
@interface AddNoteViewController : UIViewController

@property (strong, nonatomic) id AddNoteFtgnr;
@end

步骤3 AddNoteViewController.m (这是错误,它根本不在这里)

//AddNoteViewController.m
-(void)setAddNoteFtgnr:(id)AddNoteFtgnr
{

//Code that sets the property

}

2 个答案:

答案 0 :(得分:1)

您的主视图控制器的detailViewController属性可能为零。在tableview:didSelectIndexPath:方法中设置断点并检查detailViewController属性。如果您不知道如何设置断点,则在方法的开头添加一个NSLog语句,该语句在self.detailViewController中显示该值。 (并了解断点。)

答案 1 :(得分:0)

向下投票

我解决了我的问题。

在MasterviewController中,我必须初始化未发生的viewcontroller。

AddNoteViewController* addNoteViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"addNoteViewController"];

addNoteViewController.addNoteFtgNr = kundnr;