iOS: Pointer get lost when poping back to parentView
NickName:user2173784 Ask DateTime:2013-03-15T20:01:25

iOS: Pointer get lost when poping back to parentView

I code for iOS 6.1 with ARC and have a problem that my objects in the nsmutablearray get "lost". I will explain it in more detail:

  1. There is a parent view which has a NSMutableArray which should hold some objects(addresses)
  2. Everytime I hit the button the data of the Array will be displayed with NSLog and then the navigation controller pushes to the ChildView
  3. The childview generates a new address object

If I do this and then press the back button in the view and wants to try the same case (again pressing the button) the data in the array is lost and I get a EXC_BAD_ACCESS

My assumption: When I go back to parentView ARC deallocates the second view and everything in it. But my Address object will be referenced from the array so that the ARC counter for this object should not be 0.

Can anyone help me? Here is the concrete code

ParentViews Controller h:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
@property (nonatomic, strong) NSMutableArray *adresses;
@property int count;
-(void)goFurther;
@end

ParentViews Controller m:

#import "ViewController.h"
#import "SecondViewController.h"
#import "Address.h"

@interface ViewController ()
@end

@implementation ViewController

- (void)viewDidLoad
{
    self.title=@"First View";
    UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithTitle:@"Add address" style:UIBarButtonItemStylePlain
                                                                 target:self action:@selector(goFurther)];
    [self.navigationItem setLeftBarButtonItem:addButton];
    self.adresses=[[NSMutableArray alloc] init];
    self.count=0;
    [super viewDidLoad];
}

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

-(void) goFurther{
    for (Address *a in self.adresses) {
        NSLog(@"Adresse:");
        NSLog(a.street);
        NSLog(a.door);
    }
    self.count++;
    SecondViewController *second=[[SecondViewController alloc]initWithView:self];

   [self.navigationController pushViewController:second animated:true];
}

@end

ChildViews Controller h:

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

@interface SecondViewController : UIViewController
@property ViewController * viewCon;
-(id) initWithView: (ViewController*) view;
@end

ChildViews Controller m:

#import "SecondViewController.h"
#import "Address.h"

@interface SecondViewController ()

@end

@implementation SecondViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

-(id) initWithView: (ViewController*) view{
    self = [super init];
    self.viewCon=view;
    Address *a=[Address alloc];
    a.street=[NSString stringWithFormat:@"Street %d", self.viewCon.count];
    a.door=@"Door";
    [self.viewCon.adresses addObject: a];
    return self;
}

- (void)viewDidLoad
{
    self.title=@"Second View";
    [super viewDidLoad];
}

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

@end

Copyright Notice:Content Author:「user2173784」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/15431935/ios-pointer-get-lost-when-poping-back-to-parentview

More about “iOS: Pointer get lost when poping back to parentView” related questions

iOS: Pointer get lost when poping back to parentView

I code for iOS 6.1 with ARC and have a problem that my objects in the nsmutablearray get "lost". I will explain it in more detail: There is a parent view which has a NSMutableArray which should hold

Show Detail

Why the pointer fails to take new value back? iOS

Question are as follows: @property(nonatomic,strong)UIButton *startBtn; -(void)createView { int btnW = 22; int btnH = 14; self.startBtn = [[UIButton alloc] init]; NSLog(@"%@",self.startB...

Show Detail

iOS notification when connection is back

I'm developing a queue of messages for a messenger app. On Android if the connection is lost, messages is save on the queue and as soon as the 3G or wifi is back the messages go out of the queue. ...

Show Detail

how to change the background image of the cell when poping back to the viewcontroller?

I have an application in which i am changing the cell background image when the user selects each row.Like this` - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)

Show Detail

how to call a function from mainview when poping

This might be asked a few time already, how do i call a function when poping back ? NSlog show the text but nothing shows on the view after popping. list pop to table but the image are not showing

Show Detail

Passing data from childView do parentView!

I am having trouble passing data from a childView to the parentView inside a navigationController when the user click on the back button. I tried using the viewWillDisappear and it is not being ca...

Show Detail

Get back lost shelf changes

I have shelved my 26 java files changes via Intellij Idea 2016.2.1 and I checkout to different branch. When I came to old branch to check my shelved changes. I gone a mad now, I lost all the file...

Show Detail

iOS: How to get pointer to navigation controller

I'm obviously missing something... In my iOS app, I push a UIViewController onto a navigation controller: MyViewController *mvc = [[MyViewController alloc] initWithNibName:@"MyViewController"]; [...

Show Detail

rotating UIbutton, while rotating parentview they reside with in

I have one UIView that I rotate manually when orientation changes, this UIView hosts a number of buttons, I want to rotate them too based on the change in orientation. I am doing this :

Show Detail

UIView on top of parentView

I have a navigation view controller and I want a subview ( just a view and not a view controller) to slide on top of it when a button in the parent view is clicked. Now, the thing is when I do this...

Show Detail