简体   繁体   English

物业不更新

[英]Property don't update

I beginner in iOs programming. 我是iOs编程的初学者。 I have some problem. 我有问题 I have MasterViewController, code below 我有MasterViewController,下面的代码

#import "MasterViewController.h"

#import "DetailViewController.h"

@interface MasterViewController () {

}

@property (strong, nonatomic) NSString *str;
@end

@implementation MasterViewController

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

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.str = @"1234";
}

and I send property "str" to DetailViewController. 然后将属性“ str”发送到DetailViewController。

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"showDetail"]) {
        NSString *object = self.str;
        [[segue destinationViewController] setDetailItem:object];
    }
}

after, I change str 之后,我更改str

#import "DetailViewController.h"

@interface DetailViewController ()
@end

@implementation DetailViewController

- (void)setDetailItem:(id)newDetailItem
{
    if (_detailItem != newDetailItem) {
        _detailItem = newDetailItem;


    }
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.detailItem = @"1233";
} 

after, I come back to MasterViewController but str don't change. 之后,我回到MasterViewController,但是str不会改变。 Why? 为什么?

The problem is that you're just changing which object the detailItem property refers to in DetailViewController , rather than changing the value of the object itself. 问题在于您只是更改了detailItem属性在DetailViewController引用的对象,而不是更改对象本身的值。 When you set self.detailItem in the viewDidLoad method, the str property MasterViewController will continue to point to the original string: 当您在viewDidLoad方法中设置self.detailItem时, str属性MasterViewController将继续指向原始字符串:

Before: 之前:

MasterViewController.str -> 1234 <- DetailViewController.detailItem

Then you do self.detailItem = 1233 . 然后,您执行self.detailItem = 1233 It doesn't change the value of the strings themselves, but just makes detailItem point to a new string. 它不会更改字符串本身的值,而只是使detailItem指向新字符串。 Looking at the graph below, it moves the arrow pointing from DetailViewController rather than changing the content of "1234" itself: 看下面的图,它移动了从DetailViewController指向的箭头,而不是更改“ 1234”本身的内容:

MasterViewController.str -> 1234
                            1233 <- DetailViewController.detailItem

What you want to do here is use the delegate pattern to properly notify the MasterViewController of changes in the detail controller and give it access to the new value. 您要在此处执行的任务是使用委托模式将明细控制器中的更改正确地通知给MasterViewController并为其提供对新值的访问权限。

DetailViewController.h: DetailViewController.h:

// declare the delegate protocol
@class DetailViewController;
@protocol DetailViewControllerDelegate <NSObject>
    -(void)detailViewControllerChangedDetailItem:(DetailViewController *)detailController;
@end

@interface DetailViewController
// add a property for the delegate
@property (nonatomic, copy) NSString *detailItem;
@property (nonatomic, weak) id <DetailViewControllerDelegate> delegate;
@end

DetailViewController.m: DetailViewController.m:

- (void)setDetailItem:(id)newDetailItem
{
    if (_detailItem != newDetailItem) {
        _detailItem = newDetailItem;
        // notify the delegate whenever the detailItem changes
        [self.delegate detailViewControllerChangedDetailItem:self];
   }
}

MasterViewController.m: MasterViewController.m:

// mark this as implementing the delegate protocol
@interface MasterViewController () <DetailViewControllerDelegate> {
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"showDetail"]) {
        NSString *object = self.str;
        // wire up the delegate connection
        [[segue destinationViewController] setDelegate:self];
        // now whenever detailItem changes in the DetailViewController,
        // it will notify its delegate (the master VC)
        [[segue destinationViewController] setDetailItem:object];
    }
}

// implement the delegate method
-(void)detailViewControllerChangedDetailItem:(DetailViewController *)detailController
{
    // now you can get a copy of the new detailItem value 
    // and do whatever you want with it.
    self.str = detailController.detailItem;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 不要在transitionWithView中为属性设置动画 - Don't animate a property in transitionWithView TBXML tableview内容不更新 - TBXML tableview contents don`t update Sqlite插入或替换不会更新一列 - Sqlite Insert or Replace don't update one column 钱包中的 Apple Passes 不会更新推送通知 - Apple Passes in Wallet don't update on push notifications 更新后,保存的照片不会显示在设备上 - Saved photos don't appear on device after update 设置callEventHandler后,CTCallCenter不会更新currentCalls属性 - CTCallCenter doesn't update the currentCalls property after I set callEventHandler 我不知道为什么我得到EXC_BAD_ACCESS(如使用@property保留) - I don't know why I get EXC_BAD_ACCESS ( As Using @property retain ) CLLocationManager distanceFilter如何工作? 您是否不需要更新才能知道手机在哪里? - How does CLLocationManager distanceFilter work? Don't you need an update to know where the phone is? 为什么我的UILabels在将它放入scrollView之后不再更新? - Why my UILabels don't update any more after putting it in a scrollView? UITextView.text属性不会第一次更新,然后在它出现时会剪辑或不显示 - UITextView.text property doesn't update first time, and then clips or doesn't display when it does
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM