cuốn sách gpt4 ai đã làm

ios - 数组未通过委托(delegate)从 TableView 传递到 View Controller

In lại Tác giả: Walker 123 更新时间:2023-11-29 01:44:12 26 4
mua khóa gpt4 Nike

我正在尝试将数组从 TableView Controller 复制到 View Controller 。我已经多次检查代码,似乎没问题。

//Delegate class
#import

@protocol Category
@required
-(void) delegateMethodForCategory : (NSMutableArray *) arrayOfCategory;


@kết thúc

@interface Categories : UIViewController

@property (nonatomic) id delegate;
@property (nonatomic,strong) NSArray *sports;
@property (strong, nonatomic) IBOutlet UITableView *tableview;
@property (nonatomic,strong) NSMutableArray *selectedIndexes;

@kết thúc

//Delegate methods
#import "Categories.h"
@interface Categories ()
{
NSMutableArray *array ;
}
@kết thúc

@implementation Categories

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
_sports= [[NSArray alloc] initWithObjects: @"Baseball", @"Soccer", @"Hockey",
@"Other",nil];


}

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

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
trả về 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return _sports.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
array = [[NSMutableArray alloc]init];

// Configure the cell...

cell.textLabel.text=[self.sports objectAtIndex:indexPath.row];
return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];

NSString *cellText = selectedCell.textLabel.text;
if([tableView cellForRowAtIndexPath:indexPath].accessoryType==UITableViewCellAccessoryNone)
{ [tableView cellForRowAtIndexPath:indexPath].accessoryType=UITableViewCellAccessoryCheckmark;
[array addObject:cellText];

}else if([tableView cellForRowAtIndexPath:indexPath].accessoryType==UITableViewCellAccessoryCheckmark){
[tableView cellForRowAtIndexPath:indexPath].accessoryType=UITableViewCellAccessoryNone;
[array removeObject:cellText];

}


}
- (IBAction)doneButton:(id)sender {
[self.delegate delegateMethodForCategory:array];
[self dismissViewControllerAnimated:YES completion:nil];

}

@kết thúc
#import
#import "Categories.h"

@interface ActivityCreator : UIViewController
@property (nonatomic) Categories *requestClass;

@property (nonatomic,strong) NSMutableArray *arrayOfSports;

@kết thúc

//This class implements delegate
import "ActivityCreator.h"

@interface ActivityCreator ()

@kết thúc

@implementation ActivityCreator

- (void)viewDidLoad {
[super viewDidLoad];
[self settingUp];
// Do any additional setup after loading the view.
}

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


-(UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller{
return UIModalPresentationNone;
}


-(void)settingUp{
_requestClass = [[Categories alloc]init];
self.requestClass.delegate = self;

}

#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

if([segue.identifier isEqualToString:@"hubabuba"]){
Categories *pop = (Categories *)segue.destinationViewController;
pop.modalPresentationStyle = UIModalPresentationPopover;
pop.popoverPresentationController.delegate = self;

}
}

-(void) delegateMethodForCategory : (NSMutableArray *) arrayOfCategory {
_arrayOfSports = arrayOfCategory;
NSLog(@"%@",_arrayOfSports);

}

任何我做错的指导都会有很大帮助。已经坚持了一段时间。

委托(delegate)方法根本没有被调用。

Cảm ơn

câu trả lời hay nhất

在 prepareForSegue 方法中设置 Categories 类的委托(delegate),而不是在 settingUp 方法中设置。

Viết

 pop.delegate = self;

在 prepareForSegue 方法中。

关于ios - 数组未通过委托(delegate)从 TableView 传递到 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32101651/

26 4 0
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress