gpt4 book ai didi

ios - 使用 iOS 委托(delegate)

转载 作者:行者123 更新时间:2023-11-29 02:43:55 26 4
gpt4 key购买 nike

我是 iOS 及其开发人员的新手。我已经使用 iOS 委托(delegate)在 View Controller 之间传递值1. Essentialinfocontroller - 它有 TableView2. 细节 Controller -

我想将 Essentialinfocontroller 的值传递给 detailcontroller

为了做到这一点,我使用了委托(delegate),但控制台上没有打印任何内容,请帮助我。

enter image description here基本信息 Controller .h

#import <UIKit/UIKit.h>

@protocol sendTestData <NSObject>
-(void)sendDataToA:(NSArray *)array;
@end
@interface Essentialinfocontroller : UIViewController<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *info;
@property (nonatomic,strong) NSDictionary * courses;
@property (nonatomic, strong)NSArray *coursekeys;
@property(nonatomic,strong) NSString* customeLink;
@property (nonatomic,retain)NSArray * array;
@property(nonatomic,assign)id delegate;
@end

基本信息 Controller .m

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

if(indexPath.row==0){

[self giveValuestoNSmutableArray];
[delegate sendDataToA:array];
}
}

-(void)giveValuestoNSmutableArray
{
array = [NSArray arrayWithObjects:@"Jill Valentine", @"Peter Griffin", @"Meg Griffin", @"Jack Lolwut",
@"Mike Roflcoptor", @"Cindy Woods", @"Jessica Windmill", @"Alexander The Great",
@"Sarah Peterson", @"Scott Scottland", @"Geoff Fanta", @"Amanda Pope", @"Michael Meyers",
@"Richard Biggus", @"Montey Python", @"Mike Wut", @"Fake Person", @"Chair",
nil];
}

详细 Controller .h

#import <UIKit/UIKit.h>
#import "Essentialinfocontroller.h"
@interface detailcontroller : UIViewController <sendTestData>
@end

细节 Controller .m

#import "detailcontroller.h"

@interface detailcontroller ()

@end

@implementation detailcontroller

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
Essentialinfocontroller * acontollerobject=[[Essentialinfocontroller alloc] initWithNibName:@"Essentialinfocontroller" bundle:nil];
acontollerobject.delegate=self; // protocol listener
[self.navigationController pushViewController:acontollerobject animated:YES];

}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

-(void)sendDataToA:(NSArray *)array
{
for (NSString *string in array) {
NSLog(@"%@", string);
}

}


@end

最佳答案

Controller 和委托(delegate)之间的关系有点倒退。委托(delegate)用于传回信息。您的 tableViewController 是 detailController 的委托(delegate),detailController 不应在其中分配 EssentialInfoController。您基本上是在创建一个与第一个不同的全新 EssentialInfoController。

传递数据最简单的方法就是在分配 detailController 时设置它。另外,由于您使用的是 Storyboard,因此必须使用 (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 来分配detailController。

这就是我要做的:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

if(indexPath.row==0){
[self performSegueWithIdentifier:@"OpenDetailsController"];
}
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
DetailController *controller = (DetailController *)[segue destinationViewController];
NSArray *array = <your array here>;
[controller setArray:array];
}

在您的 Storyboard中,将该转场的 Storyboard转场标识符设置为“OpenDetailsController”。

委托(delegate)函数是通过协议(protocol)定义的,您可以让 DetailController 像这样调用它的委托(delegate):

[self.delegate doSomething];

并且在 EssentialInfoViewController 中你必须定义委托(delegate)函数

-(void)doSomething {
}

关于ios - 使用 iOS 委托(delegate),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25389289/

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