ios - Object became nil after view will appear -
following situation:
i got 2 views. on firstview m display event data, on seconde can edit these data. if switch seconde view , edit value , return firstview works should. if dont change data , return back, event objecct nil.
the funny part is: before switch second view user can decide(uiactionsheet) wich changes (name etc.). @ point m logging object , nil. on viewwillappear - seconde time view loads - object not nil.
i changed property attribute of event object (strong, nonatomic), when dont change object still exists..
i provide code m @ home, maybe have idea..
firstview
- (void)viewdidload { [super viewdidload]; // additional setup after loading view nib. [advthememanager customizeview:self.view]; [self initviewdata]; [self editviewstyles]; } - (void)viewwillappear:(bool)animated{ [super viewwillappear:yes]; nslog(@"view appear!!"); nslog(@"eventname: %@", eventobject.name); nslog(@"userid: %@", appuser.userid); // logs proves @ second load of view object not nil!!! 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 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 map uilongpressgesturerecognizer *longpress = [[uilongpressgesturerecognizer alloc] initwithtarget:self action:@selector(navigateme)]; longpress.minimumpressduration = 0; [eventlocation addgesturerecognizer:longpress]; //add right navbar item handles invites, setting , 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 second view load want change more touch navbar item , leads me method. here logs null(nil). if change on seconde view 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 < 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; } } }
seconde view
@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]; // additional setup after loading view 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 , push 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 file
@property (strong, nonatomic) event *eventobject; @property (strong, nonatomic) user *appuser; //other properties
second .h file
@property (strong, nonatomic) eventdto *eventdto; @property (nonatomic, readwrite) nsinteger editenabled;
i think problem when returning second view, setting object edited value , saving it. if value not edited, object nil , saving nil object when returning first view. if case add condition checking nil in second view case , save data if not nil or edited.
Comments
Post a Comment