以下情况:
我有 2 次观看。在第一个视图上我显示事件数据,在第二个视图上我可以编辑这些数据。如果我切换到我的第二视图并编辑一个值并返回到我的第一视图,一切都会正常工作。但如果我不更改此数据并返回,我的事件对象为零。
有趣的是:在我切换到我的第二个视图之前,用户可以决定(UIActionSheet)他将做哪些更改(名称等)。此时我正在记录该对象及其零。但是在我的 viewWillAppear 上-它是视图加载的第二次-该对象不是零。
我将事件对象的属性属性更改为(强,非原子),所以当我不更改某些内容时,该对象仍然存在..
我一到家就会提供一些代码,但也许你已经有了一个想法..
第一视图
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
[ADVThemeManager customizeView:self.view];
[self initViewData];
[self editViewStyles];
}
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:YES];
NSLog(@"VIEW WILL APPEAR!!");
NSLog(@"Eventname: %@", eventObject.name);
NSLog(@"USERID: %@", appUser.userID);
// Logs proves that at second load of view the object is NOT nil!!! But Check -> - (void)eventOptionSheet!!
[self initViewData];
}
- (void) initViewData{
self.invitedUserList.delegate = self;
self.invitedUserList.dataSource = self;
self.acceptedUserList.delegate = self;
self.acceptedUserList.dataSource = self;
dccon = [[DCConnector alloc]init];
NSLog(@"Eventname: %@", eventObject.name);
NSLog(@"USERID: %@", appUser.userID);
invitedUsers = [NSMutableArray arrayWithArray:[eventObject.invitedUsers allObjects]];
acceptedUsers = [NSMutableArray arrayWithArray:[eventObject.acceptedUsers allObjects]];
eventDescriptionField.text = eventObject.descrptn;
NSString *eventImageName = [NSString stringWithFormat:@"%@_%@", eventObject.name, eventObject.date];
if ([UtilitieHandler checkIfFileExistsWitchExstensio:eventImageName :@"png"]) {
[eventImage setBackgroundImage:[UtilitieHandler loadImage:[NSString stringWithFormat:@"%@.png", eventImageName]] forState:UIControlStateNormal];
}
// Replace titleView
CGRect headerTitleSubtitleFrame = CGRectMake(0, 0, 200, 44);
UIView* _headerTitleSubtitleView = [[UILabel alloc] initWithFrame:headerTitleSubtitleFrame];
_headerTitleSubtitleView.backgroundColor = [UIColor clearColor];
_headerTitleSubtitleView.autoresizesSubviews = YES;
CGRect titleFrame = CGRectMake(0, 2, 200, 24);
UILabel *titleView = [[UILabel alloc] initWithFrame:titleFrame];
titleView.backgroundColor = [UIColor clearColor];
titleView.textColor = [UIColor colorWithRed:0.98f green:0.96f blue:0.94f alpha:1.00f];
titleView.font = [UIFont fontWithName:@"OpenSans-Semibold" size:16];
titleView.textAlignment = UITextAlignmentCenter;
titleView.shadowColor = [UIColor darkGrayColor];
titleView.shadowOffset = CGSizeMake(0, -1);
titleView.text = eventObject.name;
titleView.adjustsFontSizeToFitWidth = NO;
[_headerTitleSubtitleView addSubview:titleView];
CGRect subtitleFrame = CGRectMake(0, 24, 200, 44-24);
UILabel *subtitleView = [[UILabel alloc] initWithFrame:subtitleFrame];
subtitleView.backgroundColor = [UIColor clearColor];
subtitleView.textColor = [UIColor colorWithRed:0.98f green:0.96f blue:0.94f alpha:1.00f];
subtitleView.font = [UIFont fontWithName:@"OpenSans-SemiboldItalic" size:12];
subtitleView.textAlignment = UITextAlignmentCenter;
subtitleView.shadowColor = [UIColor darkGrayColor];
subtitleView.shadowOffset = CGSizeMake(0, -1);
subtitleView.text = [NSString stringWithFormat:@"by %@", eventObject.createdBy.displayName];
subtitleView.adjustsFontSizeToFitWidth = NO;
[_headerTitleSubtitleView addSubview:subtitleView];
self.navigationItem.titleView = _headerTitleSubtitleView;
self.imageBackground.image = [[UIImage imageNamed:@"Content_Box_Small.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(10, 10, 10, 10)];
self.navigationController.navigationBar.layer.shadowOpacity =0.0f;
self.invitedUserList.backgroundColor = [UIColor clearColor];
self.acceptedUserList.backgroundColor = [UIColor clearColor];
// init location to map
CLLocationCoordinate2D coord;
coord.latitude = [eventObject.locLatitude doubleValue];
coord.longitude = [eventObject.locLongitude doubleValue];
MKCoordinateSpan span = {.latitudeDelta = 0.005, .longitudeDelta = 0.005};
MKCoordinateRegion region = {coord, span};
MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
annotation.coordinate = coord;
[eventLocation setRegion:region];
[eventLocation selectAnnotation:annotation animated:YES];
//add gesture to map
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]
initWithTarget:self
action:@selector(navigateMe)];
longPress.minimumPressDuration = 0;
[eventLocation addGestureRecognizer:longPress];
//add right navbar item that handles invites, setting and more...
UIBarButtonItem *settingsItem = [[UIBarButtonItem alloc]initWithTitle:@"" style:UIBarButtonItemStylePlain target:self action:@selector(eventOptionSheet)];
settingsItem.image = [UIImage imageNamed:@"navigation-btn-settings"];
[settingsItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIFont fontWithName:@"OpenSans-Semibold" size:12], UITextAttributeFont,nil] forState:UIControlStateNormal];
[self.navigationItem setRightBarButtonItem:settingsItem];
}
- (void)eventOptionSheet{
NSLog(@"CREATOR USER ID: %@", eventObject.createdBy.userID);
NSLog(@"APP USER ID: %@", appUser.userID);
//On my second view load I probably want change more so i touch my navbar item and it leads me to this method. But here the logs are null(nil). If Change something on my seconde view this works fine again!!
if ([eventObject.createdBy.userID isEqualToString:appUser.userID]) {
eventCreatorSheet = [[UIActionSheet alloc] initWithTitle:eventObject.name delegate:self
cancelButtonTitle:@"Cancel"
destructiveButtonTitle:@"Delete Event"
otherButtonTitles: @"Change location", @"Edit description", @"Invite more Contacts", 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];
}
}
}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
LastEventsViewController *lastEvents;
LocateEventViewController *locateEvent;
EventMembersViewController *inviteMoreMembers;
EventDescriptionViewController *eventDescriptionView;
EventDTO *updateEventDTO;
if (actionSheet == eventCreatorSheet) {
// creator sheet
switch (buttonIndex) {
case 0:
//Remove Event clicked
[dccon deleteEvent:eventObject];
lastEvents = [[LastEventsViewController alloc]init];
[[self navigationController]popToRootViewControllerAnimated:YES];
break;
case 1:
//Edit location
locateEvent = [[LocateEventViewController alloc]init];
updateEventDTO = [[EventDTO alloc]init];
[updateEventDTO setDTOWithEntity:eventObject];
locateEvent.eventDTO = updateEventDTO;
locateEvent.editEnabled = 1;
[[self navigationController]pushViewController:locateEvent animated:YES];
break;
case 2:
//Edit description
eventDescriptionView = [[EventDescriptionViewController alloc]init];
updateEventDTO = [[EventDTO alloc]init];
[updateEventDTO setDTOWithEntity:eventObject];
eventDescriptionView.eventDTO = updateEventDTO;
eventDescriptionView.editEnabled = 1;
[[self navigationController]pushViewController:eventDescriptionView animated:YES];
break;
case 3:
//Invite more Contacts, check if numberOfInvites is < 50
break;
}
}
if (actionSheet == eventUserInvitedSheet) {
//member sheet
switch (buttonIndex) {
case 0:
//join Event clicked
NSLog(@"Event test: %@, appUserTest: %@", eventObject.name, appUser.userID);
[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;
}
}
}
第二视图
@interface EventDescriptionViewController (){
DCConnector *dccon;
}
@end
@implementation EventDescriptionViewController
@synthesize imageBackground, descriptionTextField, editEnabled, eventDTO;
- (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 from its nib.
[ADVThemeManager customizeView:self.view];
[self initViewData];
[self styleViews];
}
- (void) initViewData{
UILabel *labelTitle = [[UILabel alloc] initWithFrame:CGRectZero];
labelTitle.backgroundColor = [UIColor clearColor];
labelTitle.textColor = [UIColor colorWithRed:0.98f green:0.96f blue:0.94f alpha:1.00f];
labelTitle.font = [UIFont fontWithName:@"OpenSans-Semibold" size:16];
labelTitle.text = @"Event description";
[labelTitle sizeToFit];
UIView *viewTitle = [[UIView alloc] initWithFrame:labelTitle.bounds];
CGRect frameLbl = labelTitle.bounds;
viewTitle.frame = frameLbl;
[viewTitle addSubview:labelTitle];
[self.navigationItem setTitleView:viewTitle];
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background-tbl"]];
self.imageBackground.image = [[UIImage imageNamed:@"Content_Box_Small.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(10, 10, 10, 10)];
if (editEnabled == 1) {
UIBarButtonItem *newEventToggleView = [[UIBarButtonItem alloc]initWithTitle:@"Save" style:UIBarButtonItemStylePlain target:self action:@selector(updateEvent)];
[newEventToggleView setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIFont fontWithName:@"OpenSans-Semibold" size:12.0], UITextAttributeFont,nil] forState:UIControlStateNormal];
[self.navigationItem setRightBarButtonItem:newEventToggleView];
descriptionTextField.text = eventDTO.description;
}else{
UIBarButtonItem *newEventToggleView = [[UIBarButtonItem alloc]initWithTitle:@"Next" style:UIBarButtonItemStylePlain target:self action:@selector(initNextEventView)];
[newEventToggleView setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIFont fontWithName:@"OpenSans-Semibold" size:12.0], UITextAttributeFont,nil] forState:UIControlStateNormal];
[self.navigationItem setRightBarButtonItem:newEventToggleView];
}
descriptionTextField.font = [UIFont fontWithName:@"OpenSans-Semibold" size:14];
}
- (void)styleViews{
[self.descriptionTextField.layer setBorderWidth:2.0];
[self.descriptionTextField.layer setBorderColor:[[UIColor colorWithRed:(107/255.0) green:(96/255.0) blue:(84/255.0) alpha:1.00f] CGColor]];
self.descriptionTextField.layer.cornerRadius = 5;
self.descriptionTextField.clipsToBounds = YES;
}
- (void)updateEvent{
eventDTO.description = self.descriptionTextField.text;
//Edit Event and push it to all members
dccon = [[DCConnector alloc]init];
[dccon updateEvent:eventDTO];
EventOverviewController *overview = (EventOverviewController*)[self.navigationController.viewControllers objectAtIndex:1];
overview.eventObject = (Event*)[dccon getObjectWithID:eventDTO.entitysID];
overview.appUser = [dccon getAppUser];
[[self navigationController]popToViewController:overview animated:YES];
}
Firstview .h 文件
@property (strong, nonatomic) Event *eventObject;
@property (strong, nonatomic) User *appUser;
//Other properties
第二个 .h 文件
@property (strong, nonatomic) EventDTO *eventDTO;
@property (nonatomic, readwrite) NSInteger editEnabled;