我会尝试正确解释我的情况。
我有 2 次观看。在我的第一个视图中,我得到了一张列出事件的表格。如果我触摸一个单元格,我会得到我的 EventOverviewView。所以我对每个视图都有不同的行为:
FirstView(事件列表):
如果我触摸并按住其中一个单元格,我会显示 UIActionSheet。在那里我可以说我想加入那个事件,但是在显示工作表之前,我从数组中获取选定的事件并将其保存在一个临时变量中。所以在那个观点我没有任何问题。一切都正确保存(我在我的 SQLite 浏览器中看到它)。
但正如我之前所说,我可以说我想在决定是否加入之前看到事件,此时我推送到提到的EventOverviewView(事件对象设置为属性并且不为零,我测试了它)。在这个观点上,我也可以决定,如果我想加入,这里我们遇到了我的问题(以下处理将用户添加到事件的方法)。
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
EventOverviewController *overView;
if (actionSheet == eventUserInvitedSheet) {
//member sheet
switch (buttonIndex) {
case 0:
//join Event clicked //This method is executed and saved correctly in cd!!!
[dccon addAcceptedUserToEvent:tmpEventForActionSheet :appUser];
break;
case 1:
//Decline Event
[dccon removeInvitedUserFromEvent:tmpEventForActionSheet :appUser];
[dccon deleteEvent:tmpEventForActionSheet];
break;
case 2:
//show event
overView = [[EventOverviewController alloc]init];
overView.eventObject = tmpEventForActionSheet;
overView.appUser = appUser;
overView.hidesBottomBarWhenPushed = YES;
[[self navigationController]pushViewController:overView animated:YES];
break;
case 3:
//chat creator
break;
}
totalEvents = [dccon getEvents];
[eventsTable reloadData];
}
}
该方法正确执行,正如您所见,我在案例 2 上推送了我的视图。在 EventOverviewView 上,我得到了一个 navbaritem,我在该视图上执行我的工作表:
根据 appUser 的角色创建不同的工作表:
- (void)eventOptionSheet{
if ([eventObject.createdBy.userID isEqualToString:appUser.userID]) {
eventCreatorSheet = [[UIActionSheet alloc] initWithTitle:eventObject.name delegate:self
cancelButtonTitle:@"Cancel"
destructiveButtonTitle:@"Delete Event"
otherButtonTitles: nil];
eventCreatorSheet.actionSheetStyle = UIActionSheetStyleAutomatic;
[eventCreatorSheet showFromTabBar:self.tabBarController.tabBar];
}else{
NSSet *invitedSet = eventObject.invitedUsers;
NSSet *acceptedSet = eventObject.acceptedUsers;
BOOL existsInInvitedList = [[invitedSet valueForKeyPath:@"objectID"] containsObject:appUser.objectID];
BOOL existsInAcceptedList = [[acceptedSet valueForKeyPath:@"objectID"] containsObject:appUser.objectID];
if (existsInInvitedList) {
eventUserInvitedSheet = [[UIActionSheet alloc] initWithTitle:eventObject.name delegate:self
cancelButtonTitle:@"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:@"Join", @"Decline", @"Chat creator", nil];
eventUserInvitedSheet.actionSheetStyle = UIActionSheetStyleAutomatic;
[eventUserInvitedSheet showFromTabBar:self.tabBarController.tabBar];
}else if (existsInAcceptedList){
eventUserAcceptedSheet = [[UIActionSheet alloc] initWithTitle:eventObject.name delegate:self
cancelButtonTitle:@"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:@"Revoke invite", @"Chat creator", nil];
eventUserAcceptedSheet.actionSheetStyle = UIActionSheetStyleAutomatic;
[eventUserAcceptedSheet showFromTabBar:self.tabBarController.tabBar];
}
}
}
eventObject 是我从列表视图传递的对象
好吧,让我们看看我的工作表操作:
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
LastEventsViewController *lastEvents;
if (actionSheet == eventCreatorSheet) {
// creator sheet
switch (buttonIndex) {
case 0:
//Remove Event clicked
[dccon deleteEvent:eventObject];
lastEvents = [[LastEventsViewController alloc]init];
[[self navigationController]popToRootViewControllerAnimated:YES];
break;
}
}
if (actionSheet == eventUserInvitedSheet) {
//member sheet
switch (buttonIndex) {
case 0:
//join Event clicked, logs proves that nothing is nil
NSLog(@"Event test: %@, appUserTest: %@", eventObject.name, appUser.userID);
// same like in first view, but don't save :(
[dccon addAcceptedUserToEvent:eventObject :appUser];
invitedUsers = [NSMutableArray arrayWithArray:[eventObject.invitedUsers allObjects]];
acceptedUsers = [NSMutableArray arrayWithArray:[eventObject.acceptedUsers allObjects]];
[acceptedUserList reloadData];
[invitedUserList reloadData];
break;
case 1:
//Decline Event
[dccon removeInvitedUserFromEvent:eventObject :appUser];
[dccon deleteEvent:eventObject];
lastEvents = [[LastEventsViewController alloc]init];
[[self navigationController]pushViewController:lastEvents animated:YES];
break;
case 2:
//chat creator
break;
}
}
if (actionSheet == eventUserAcceptedSheet) {
//member sheet
switch (buttonIndex) {
case 0:
//leave Event clicked
[dccon removeAcceptedUserFromEvent:eventObject :appUser];
[dccon deleteEvent:eventObject];
lastEvents = [[LastEventsViewController alloc]init];
[[self navigationController]pushViewController:lastEvents animated:YES];
break;
case 1:
//chat creator
break;
}
}
if (actionSheet == changeImageSheet) {
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.allowsEditing = YES;
switch (buttonIndex) {
case 0:
//Remove user clicked
imagePickerController.delegate = self;
imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentModalViewController:imagePickerController animated:YES];
break;
case 1:
//Show Creator
imagePickerController.delegate = self;
imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:imagePickerController animated:YES];
break;
}
}
}
最后但并非最不重要的添加方法:
- (BOOL) addAcceptedUserToEvent:(Event*)event :(User*)invitedUser{
BOOL saved = YES;
[self getDataCoreContext];
if (context) {
[event removeInvitedUsersObject:invitedUser];
[event addAcceptedUsersObject:invitedUser];
NSError *error;
if (![context save:&error]) {
NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
saved = NO;
}
}
return saved;
}
好吧,我真的很困惑,为什么相同的方法适用于我的第一个视图但不适用于我的第二个视图。我真的希望我能正确解释。ty 已经有任何建议了!
编辑1: 返回的方法,上下文并将其分配给类变量:
@implementation DCConnector{
NSManagedObjectContext *context;
UtilitieHandler *utilitie;
}
- (NSManagedObjectContext *) getDataCoreContext{
/** one line complete init */
CoreDataStack* cdStack = [CoreDataStack coreDataStackWithModelName:@"Model"];
/** one property you need for all Core Data method calls */
if (context == nil) {
context = cdStack.managedObjectContext;
}
return context;
}
编辑 2: 我激活了 SQLite 调试功能,以查看插入语句是否会进入核心数据,但就像行为所证明的那样。它适用于第一个但在第二个视图上没有插入查询是触发器..