gpt4 book ai didi

iphone - 属性不更新

转载 作者:行者123 更新时间:2023-11-29 03:42:03 24 4
gpt4 key购买 nike

我是 iOs 编程的初学者。我有一些问题。我有 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";
}

我将属性“str”发送到DetailViewController。

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

之后,我改变了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";
}

之后,我回到 MasterViewController 但 str 没有改变。为什么?

最佳答案

问题是您只是更改 detailItem 属性在 DetailViewController 中引用的对象,而不是更改对象本身的值。当你在viewDidLoad方法中设置self.detailItem时,str属性MasterViewController将继续指向原来的字符串:

之前:

MasterViewController.str -> 1234 <- DetailViewController.detailItem

然后你执行self.detailItem = 1233。它不会更改字符串本身的值,而只是使detailItem指向一个新字符串。看下图,它移动了从 DetailViewController 指向的箭头,而不是更改“1234”本身的内容:

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

您想要在这里做的是使用 delegate模式以正确通知 MasterViewController 详细信息 Controller 中的更改并为其提供对新值的访问权限。

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:

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

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;
}

关于iphone - 属性不更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18292298/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com