Resolved Pushing Data (Array) to detail view

Discussion in 'iOS Programming' started by Mvkoe, Dec 29, 2012.

  1. Mvkoe, Dec 29, 2012
    Last edited: Jan 3, 2013

    Mvkoe macrumors member

    Mvkoe

    Joined:
    Aug 4, 2008
    Location:
    Belgium
    #1
    Hey

    I'm trying to get a hang of iOS programming and Objective-C by trying to realize my app idea.

    From now on everything is going very well. I've got an Navigation controller, Tableview and a subview when a row is pressed.

    I'm getting my data from a plist and putting it in an Array first:

    Code:
        NSString *path = [[NSBundle mainBundle] pathForResource:@"file" ofType:@"plist"];
        NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];
        
        self.rootControllerData = dict;
        
        NSArray *array = [[self.rootControllerData allKeys] sortedArrayUsingSelector:@selector(compare:)];
        
        self.diceNamen = array;
    So i'm populating my tableview with the "diceNamen". en then when a row is selected at intexPath:

    Code:
    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
        DicingViewController *dicingView = [[DicingViewController alloc] initWithNibName:@"DicingViewController" bundle:nil];
        
        dicingView.clickedDice = [self.diceNamen objectAtIndex:indexPath.row];
        
        dicingView.dicingTemp= [[NSArray alloc] initWithObjects:[rootControllerData objectForKey:[self.diceNamen objectAtIndex:indexPath.row]], nil];
        
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
        
        [self.navigationController pushViewController:dicingView animated:YES];
        
    }
    So I'm basicly pushing my data to my other controller and putting the clickedDice (Name that i've clicked in my tableview) to my title and then getting the strings with it that are in my plist file.

    My plist file is setted up like this:

    Code:
    Dictionary
    -Array
    --String
    --String
    -Array
    --String
    aso..
    So the problem is that when i'm trying to use the array that i've pushed to my other view, It's all in one object. so there are 3 string in an array in my second view array ..

    Does anyone know a better way to do this ? Or a solution for getting 3 strings out of that array and putting it in one array.

    I'm tried it with a for loop, but it keeps giving me (null)..

    Grz
     
  2. larswik macrumors 68000

    Joined:
    Sep 8, 2006
    #2
    Well null is an indication that something does not exist or have no space in memory so it can't find it. Even though I have been working with Objective C for a while now I might be wrong here but I don't think you can do this.

    Code:
     dicingView.dicingTemp= [[NSArray alloc] initWithObjects:[rootControllerData objectForKey:[self.diceNamen objectAtIndex:indexPath.row]], nil];
    
    You instantiated a new object a couple lines above from that Class, here.

    Code:
    DicingViewController *dicingView = [[DicingViewController alloc] initWithNibName:@"DicingViewController" bundle:nil];
    
    When that Object is created everything should be initialized to use but now you are trying to alloc / init an array in VC2 from VC1, you can't do that from what I know. I don't have the solution for you but you are needing to push an array pointer forward to the next viewController for that new controller to use.

    Hope someone with better knowledge chimes in too. I had something similar to this a couple months ago that also returned null for me.

    Good Luck
     
  3. Mvkoe thread starter macrumors member

    Mvkoe

    Joined:
    Aug 4, 2008
    Location:
    Belgium
    #3
    Thanks!

    But when I do it on my way (I've included the VC2 so i can do dicingView.), I can set an Array in my Array. I can see that it's populated with the things i Push, But the 3 strings are in one Array Object. And don't know how i can seperate it from each other. When using a for loop i can't seem to fill my new array in me VC2
     
  4. larswik macrumors 68000

    Joined:
    Sep 8, 2006
    #4
    Wait.. So are you saying you are able to alloc / init and NSArray in VC2 from VC1? I would not think that is possible.

    If you are wanting to get you 3 string objects out of an array use a for loop. like, for (int i = 0 ; i < myArray.count ; i++). Or what I started using fast enumeration,
    Code:
    for (NSString *aKey in myArray){
         //do something with my aKey string....
    }
    
     
  5. Mvkoe, Dec 30, 2012
    Last edited: Dec 30, 2012

    Mvkoe thread starter macrumors member

    Mvkoe

    Joined:
    Aug 4, 2008
    Location:
    Belgium
    #5
    Yeah it's possible because of this line:

    Code:
    DicingViewController *dicingView = [[DicingViewController alloc] initWithNibName:@"DicingViewController" bundle:nil];
    So i can send my array from VC1 to VC2.

    and i'm going to try that code now!

    EDIT:

    I'm very close now! I have 2 labels named "something1 and something2". how can I make it possible in my for loop saying something like this:
    Code:
    for (i = 0, i < [array count], i++){
    something[i].text = @"bla";
    }
    
     
  6. larswik macrumors 68000

    Joined:
    Sep 8, 2006
    #6
    Interesting..... I didn't think that was possible. You create your object

    Code:
    DicingViewController *dicingView = [[DicingViewController alloc] initWithNibName:@"DicingViewController" bundle:nil];
    
    Then using @properties and @synthisize you create the setters and getters to access your instance variables in that new object. I would think that you would alloc/init your Array in the init method of your new class?

    I could be wrong or something that I have not learned yet.
     
  7. Mvkoe thread starter macrumors member

    Mvkoe

    Joined:
    Aug 4, 2008
    Location:
    Belgium
    #7
    I've edited my last post with a new question!
     
  8. larswik macrumors 68000

    Joined:
    Sep 8, 2006
    #8
    myLable.text = [myArray objectAtIndex:i];

    You got me curious about the way you are doing it so I am testing it right now too.
     
  9. larswik macrumors 68000

    Joined:
    Sep 8, 2006
    #9
    I get (null) when I NSLog my array from the second VC pushed on the stack. in my first VC they push a button

    Code:
    -(IBAction)pushButton:(id)sender{
        VC2 *secondViewController = [[VC2 alloc] initWithNibName:@"VC2" bundle:nil];
        [self.view addSubview:secondViewController.view];
        secondViewController.myArrayVC2 = [[NSArray alloc] initWithObjects:@"Hello World", nil];
    }
    Then in my second VC in the viewDidLoad method I added this code. I put a simple UILable on the screen and connected it. But when I write out my NSLog it says myArray is (null). This means it never got instantiated by trying to alloc/init it from the first VC.

    Code:
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        self.myLable = [myArrayVC2 objectAtIndex:0];
        NSLog(@"myARray: %@", myArrayVC2);
    }
    
    The way I have gotten arrays in a new VC is to write my data to a plist and then pass a string in to my new VC and use that string to retrieve the data from the plist into an NSDictionary and using that string as the key to retrieve the array I needed.

    I am by far an expert at programming but I don't think you are going to get it to function like your doing. Or the other way, that I learn resiliently are Singletons. Create the NSMutableArray in your AppDelegate and then pass the data to it as you exit the frist VC and then retrive it in the AppDelegate in the secondVC.



    I could be wrong but I don't think so.
     
  10. Mvkoe thread starter macrumors member

    Mvkoe

    Joined:
    Aug 4, 2008
    Location:
    Belgium
    #10
    Yeah i got that to but just say that i have 100 labels (label0 - label100) en i need to set 100 texts.

    So i was thinking about a for loop like:

    Code:
    for (i=0, i < [myArray count], i++){
    label[i].text = [myArray objectAtIndex:i]
    }
    But he says an error at the label. So how would i do this.

    Furter, maybe your right about the VC1 and 2, but i did sended my array to my new view on that way. Don't ask me to much techical stuff because I just thinked and tried how i could do it.

    I'll try using the way you do it :D
     
  11. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #11
    I think you should consider setting up a model class that holds the information of interest to you and then use instances of that when you want to pass that information around.
     
  12. larswik macrumors 68000

    Joined:
    Sep 8, 2006
    #12


    dejo has a good point. It sounds to me from reading your replies and code that you are some what new to programming and maybe you should take a step back and learn a little bit more about how things work to help you out and make this stuff a bit easier.

    This is a very familiar thing to me because I was doing it too when learning. I was moving a head much faster then I was able to comprehend what I was doing. Even when I got a piece of code to help me a long on a project I still didn't get what was happening with the code even though it was working. This will just lead to bigger problem down the road for you.

    Are you reading any books right now to learn programming?
     
  13. Mvkoe thread starter macrumors member

    Mvkoe

    Joined:
    Aug 4, 2008
    Location:
    Belgium
    #13
    Yeah I'm reading "Beginning iOS 5 programming" and already read "Learn objective-c on mac". I have a Java background, and some things that i want to do in Objective-C doesn't always are the same like in Java, so I'm just asking those things also straight forward for the sollution so i can build upon that.

    But yeah maybe I should take it step by step
     
  14. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #14
    How would you handle this issue using Java?
     
  15. Mvkoe thread starter macrumors member

    Mvkoe

    Joined:
    Aug 4, 2008
    Location:
    Belgium
    #15
    Trying to think back at my Java background, hmm I think it needs to be solved by putting the labels in an Array ?
     
  16. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #16
    In Java, I think a JTable and a suitable TableModel would work a lot better than trying to simulate a table with a bunch of labels in an array.

    http://docs.oracle.com/javase/tutorial/uiswing/components/table.html

    Can you list the Java GUI classes you're familiar with, because it seems like part of the problem you're having is unfamiliarity with complex interactive classes composed of simpler elements. That is, JTable is more than just an array of JLabels, even when its cells contain labels.

    I only referred to JTable as a more suitable Java class, not because it directly corresponds to the iOS table view class. It doesn't. There are many differences between the two, so you should study the iOS class, rather than studying JTable and inferring that the iOS class works the same way.
     

Share This Page