gpt4 book ai didi

ios - 自定义委托(delegate)在调用方法时变为nil

转载 作者:行者123 更新时间:2023-11-29 10:40:52 25 4
gpt4 key购买 nike

我想要的是,当我向数据库添加新数据时,我使用的 TableView 应该自动重新加载。

我的 TableView 在 MasterViewController 中。 UserDetailUI 包含用于将数据存储在数据库中的表单。

这是我的代码::

MasterViewController.h

#import <UIKit/UIKit.h>
#import "DBManager.h"
#import "UserDetailsUi.h"

@interface MasterViewController : UITableViewController<UITableViewDataSource,UITableViewDelegate,ReloadDataDelegate>
{
UserDetailsUi *userDetail;
DBManager *db;
NSMutableArray *firstName;
NSMutableArray *lastName;
NSMutableArray *profileData;
NSMutableArray *rowId;
}

MasterViewController.m

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

self.tableView.delegate=self;
self.tableView.dataSource=self;

// Conferming Delegate
userDetail = [self.storyboard instantiateViewControllerWithIdentifier:@"UserDetails"];
// here i got the reference of UserDetailUI
userDetail.delegate=self;
// here i got the reference of masterviewController in delegate property.


db = [DBManager getSharedDatabase];
NSString *createTableQuery = @"create table if not exists USER_MASTER (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE, FirstName TEXT, LastName TEXT, ProfilePic TEXT)";
[db createTable:createTableQuery];

self.navigationItem.leftBarButtonItem = self.editButtonItem;

UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(insertNewObject:)];
self.navigationItem.rightBarButtonItem = addButton;


[self downloadDataFromDatabase];
[self.tableView reloadData];
}

-(void)downloadDataFromDatabase{
NSString *selectQuery = @"select * from USER_MASTER";
NSDictionary *data = [db retriveAllData:selectQuery];
int numberOfRows = data.count;
NSArray *keys = [[NSArray alloc]initWithArray:[data allKeys]];

firstName = [[NSMutableArray alloc]init];
lastName = [[NSMutableArray alloc]init];
profileData = [[NSMutableArray alloc]init];
rowId = [[NSMutableArray alloc]init];

for (int counter; counter<numberOfRows; counter++) {
NSDictionary *currentRow = [[NSDictionary alloc]initWithDictionary:[data objectForKey:[keys objectAtIndex:counter]]];
[firstName addObject:[currentRow objectForKey:@"firstname"]];
[lastName addObject:[currentRow objectForKey:@"lastname"]];
[profileData addObject:[currentRow objectForKey:@"ProfilePicture"]];
[rowId addObject:[currentRow objectForKey:@"RowId"]];
}
}
- (void)insertNewObject:(id)sender {
[self performSegueWithIdentifier:@"AddDetails" sender:nil];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:@"showDetail"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
NSDate *object = self.objects[indexPath.row];
[[segue destinationViewController] setDetailItem:object];
}
}
-(void)reloadTableData{
// This is my delegate method
[self.tableView reloadData];
}

UserDetailsUi.h

#import <UIKit/UIKit.h>
#import "DBManager.h"

@protocol ReloadDataDelegate <NSObject>

@required
-(void)reloadTableData;
@end
@interface UserDetailsUi : UIViewController<UIImagePickerControllerDelegate,UINavigationControllerDelegate>{
UIImagePickerController *imageChooser;
__weak IBOutlet UIImageView *Profile_Pic;
__weak IBOutlet UITextField *lnameTf;
__weak IBOutlet UITextField *firstNameTf;
}
- (IBAction)addDetailsClicked:(id)sender;
@property (nonatomic,weak) id<ReloadDataDelegate> delegate;
@end

UserDetailsUi.m

#import "UserDetailsUi.h"

@implementation UserDetailsUi
- (IBAction)addDetailsClicked:(id)sender {

DBManager *db = [DBManager getSharedDatabase];
NSString *profileEncoded = [self encodeToBase64String:Profile_Pic.image];
NSString *query = [NSString stringWithFormat:@"INSERT INTO USER_MASTER (FirstName,LastName,ProfilePic) values ('%@','%@','%@');",firstNameTf.text,lnameTf.text,profileEncoded];
[db insert:query];

// delegate method call to reload table view

[self.navigationController popViewControllerAnimated:YES];

// at the time of execution this delegate object remains nil
[_delegate reloadTableData];
}

最佳答案

您需要在 prepareForSegue 而不是 viewDidLoad 中设置委托(delegate)。您在 viewDidLoad 中创建的详细信息 Controller 实例与 segue 创建的实例不同,这就是您的委托(delegate)为零的原因。

关于ios - 自定义委托(delegate)在调用方法时变为nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24623650/

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