views:

785

answers:

3

Hi I'm new to the IPhoneSDK so please bear with me.

What I'm basically trying to do is I have one view with 10 buttons, named 'one' to 'ten' respectively. What I want to happen is that when I click on a button (eg: 'nine'), it'll go to another view, with animation, then on that next view, there's a label that will display the text corresponding on which button I previously clicked. (eg: I pressed 'nine' button, on the next page, the label will say 'nine')

If it's not much of a hassle, it would be greatly appreciated how each concept of yours works as well.

I basically have my own code but it's surely confusing and wrong.

Conceptually, what I tried and learned so far is:

1.) I tried animating and switching views using addSubviews, insert subviews, setAnimations etc. I managed to switch views but I don't know how to pass the data.

2.) I learned pushViewControllers. From the tutorials I saw, I practiced on having a hierarchal table. I learned that only UIViewControllers and UITableViewControllers are the only ones that can be pushed. I know how to apply it if it's in a table but I don't know how to apply it in views (because my app has the buttons randomly scattered across the screen)

If you can give me a rough sample of what I'm trying to do, even at least for the step-by-step concept, I would greatly appreciate it and will thank you all my life. I've been working on this for weeks. and not having an actual person to explain it to me properly is difficult.

Please and thank you kind sir/maam

+1  A: 

Try using tags. Give a tag to each of your buttons (1 to 10) and when a button is clicked, get the tag of the sender. From this you can know which button was clicked. Please note, this is only needed when you use the same method for all button clicks.

In the next viewcontroller to be displayed, create a property (say clickedButton) of type NSString and synthesize it in the .m file.

After getting the text in a variable based on the button clicked, create a new object of the viewcontroller to be displayed (I suppose you already do this) and set the property clickedButton of this viewcontroller object.

NewViewController *nController = [[NewViewController alloc] initWithNibName:@"NewView"];
[nController setClickedButton:yourStringVariableWithButtonName];
[[self navigationController] pushViewController:nController animated:YES];

In NewViewController, set the text of the label to clickedButton

[yourLabel setText:clickedButton];

Hope that helps.

lostInTransit
when you referred to navigationController, do i type in "navigationcontroller" itself or does that supposed to signify a controller of mine that serves a navigation controller?
Dan
also, I've put the code stub you provided in a -(IBAction)buttonClicked;am i doing it right?
Dan
navigationController is the reference you get for UINavigationController in a navigation-based application. Put in the type of app you use and the code you have included in your app so we can help with where u might be going wrong. Do you have a NewViewController?
lostInTransit
i had a question but it's too long as a comment. please refer to the latest answer to this question, thanks!
Dan
A: 

It's ok now! i totally understand the concepts of viewcontrollers and navigation controllers!

One question though. I have this code that I plan to assign in my buttons. I just don't know how to get the tag from each buttons so I can use it:

-(IBAction) loadZodiac: (NSInteger)tag {

NSString *zodiac;

switch (tag) {
 case 1:
  zodiac = @"Aries";
  break;
 case 2:
  zodiac = @"Taurus";
  break;
 case 3:
  zodiac = @"Gemini";
  break;
 case 4:
  zodiac = @"Cancer";
  break;
 case 5:
  zodiac = @"Leo";
  break;
 case 6:
  zodiac = @"Virgo";
  break;
 case 7:
  zodiac = @"Libra";
  break;
 case 8:
  zodiac = @"Scorpio";
  break;
 case 9:
  zodiac = @"Sagittarius";
  break;
 case 10:
  zodiac = @"Capricorn";
  break;
 case 11:
  zodiac = @"Aquarius";
  break;
 case 12:
  zodiac = @"Pisces";
  break;
 default:
  zodiac = @"None Selected";
  break;
}
lovescopesReading = [[LovescopesReading alloc] initWithNibName:@"LovescopesReading" bundle:nil];
lovescopesReading.title = zodiac;
lovescopesReading.message = zodiac;

Lovescopes6AppDelegate *delegate = [[UIApplication sharedApplication] delegate];
[delegate.navController pushViewController:lovescopesReading animated:YES];

}

Dan
A: 
NSString *zodiac;

for (UIButton *button in [self.view subviews]) {
    switch (button.tag) {
        case 1:
                zodiac = @"Aries";
                break;
        case 2:
                zodiac = @"Taurus";
                break;
        ....
        }

}
nathanjosiah