Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Dec 29, 2012, 08:37 AM   #1
Mvkoe
macrumors member
 
Join Date: Aug 2008
Location: Belgium
Pushing Data (Array) to detail view

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

Last edited by Mvkoe; Jan 3, 2013 at 02:20 PM.
Mvkoe is offline   0 Reply With Quote
Old Dec 29, 2012, 03:48 PM   #2
larswik
macrumors 65816
 
Join Date: Sep 2006
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
__________________
I know more than yesterday.
Lars
larswik is offline   0 Reply With Quote
Old Dec 29, 2012, 03:55 PM   #3
Mvkoe
Thread Starter
macrumors member
 
Join Date: Aug 2008
Location: Belgium
Quote:
Originally Posted by larswik View Post
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
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
Mvkoe is offline   0 Reply With Quote
Old Dec 29, 2012, 08:21 PM   #4
larswik
macrumors 65816
 
Join Date: Sep 2006
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....
}
__________________
I know more than yesterday.
Lars
larswik is offline   0 Reply With Quote
Old Dec 30, 2012, 04:00 AM   #5
Mvkoe
Thread Starter
macrumors member
 
Join Date: Aug 2008
Location: Belgium
Quote:
Originally Posted by larswik View Post
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....
}
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";
}

Last edited by Mvkoe; Dec 30, 2012 at 04:59 AM.
Mvkoe is offline   0 Reply With Quote
Old Dec 30, 2012, 04:39 PM   #6
larswik
macrumors 65816
 
Join Date: Sep 2006
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.
__________________
I know more than yesterday.
Lars
larswik is offline   0 Reply With Quote
Old Dec 30, 2012, 05:00 PM   #7
Mvkoe
Thread Starter
macrumors member
 
Join Date: Aug 2008
Location: Belgium
I've edited my last post with a new question!
Mvkoe is offline   0 Reply With Quote
Old Dec 30, 2012, 05:11 PM   #8
larswik
macrumors 65816
 
Join Date: Sep 2006
Quote:
Originally Posted by Mvkoe View Post
I've edited my last post with a new question!
myLable.text = [myArray objectAtIndex:i];

You got me curious about the way you are doing it so I am testing it right now too.
__________________
I know more than yesterday.
Lars
larswik is offline   0 Reply With Quote
Old Dec 30, 2012, 05:32 PM   #9
larswik
macrumors 65816
 
Join Date: Sep 2006
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.
__________________
I know more than yesterday.
Lars
larswik is offline   0 Reply With Quote
Old Dec 31, 2012, 03:53 AM   #10
Mvkoe
Thread Starter
macrumors member
 
Join Date: Aug 2008
Location: Belgium
Quote:
Originally Posted by larswik View Post
myLable.text = [myArray objectAtIndex:i];

You got me curious about the way you are doing it so I am testing it right now too.
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[i]. 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
Mvkoe is offline   0 Reply With Quote
Old Dec 31, 2012, 10:18 AM   #11
dejo
Moderator
 
dejo's Avatar
 
Join Date: Sep 2004
Location: The Centennial State
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.
__________________
dejo is offline   0 Reply With Quote
Old Dec 31, 2012, 03:25 PM   #12
larswik
macrumors 65816
 
Join Date: Sep 2006
Quote:
Originally Posted by Mvkoe View Post
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[i]. 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 technical stuff because I just thinked and tried how i could do it.

I'll try using the way you do it
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?
__________________
I know more than yesterday.
Lars
larswik is offline   0 Reply With Quote
Old Jan 1, 2013, 09:34 AM   #13
Mvkoe
Thread Starter
macrumors member
 
Join Date: Aug 2008
Location: Belgium
Quote:
Originally Posted by larswik View Post
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?
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
Mvkoe is offline   0 Reply With Quote
Old Jan 1, 2013, 12:32 PM   #14
dejo
Moderator
 
dejo's Avatar
 
Join Date: Sep 2004
Location: The Centennial State
Quote:
Originally Posted by Mvkoe View Post
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.
How would you handle this issue using Java?
__________________
dejo is offline   0 Reply With Quote
Old Jan 1, 2013, 12:34 PM   #15
Mvkoe
Thread Starter
macrumors member
 
Join Date: Aug 2008
Location: Belgium
Quote:
Originally Posted by dejo View Post
How would you handle this issue using Java?
Trying to think back at my Java background, hmm I think it needs to be solved by putting the labels in an Array ?
Mvkoe is offline   0 Reply With Quote
Old Jan 1, 2013, 12:46 PM   #16
chown33
macrumors 603
 
Join Date: Aug 2009
Quote:
Originally Posted by Mvkoe View Post
Trying to think back at my Java background, hmm I think it needs to be solved by putting the labels in an Array ?
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/tutori...nts/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.
chown33 is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Table View to Detail View hammy35 iPhone/iPad Programming 3 Apr 16, 2014 01:33 AM
Edit Detail View Question ArtOfWarfare iPhone/iPad Programming 5 Jan 15, 2014 06:40 PM
Detail View Question RagingGoat iPhone/iPad Programming 3 Jun 10, 2013 08:46 AM
Updating Detail View with Annotation Info RagingGoat iPhone/iPad Programming 14 May 9, 2013 01:22 PM
Pushing to a Detail View from a Table View Cell using Xcode Storyboard xcodeApp10 iPhone/iPad Programming 2 Sep 17, 2012 03:45 PM

Forum Jump

All times are GMT -5. The time now is 01:25 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC