Quiz app -next to display after Image result hides - ios

I am new to App Development, sorry if I am asking silly questions :(
I am developing a Quiz App. Whenever I give a right or wrong answer, an image appears with right or wrong answer result, however it stays for a few seconds, but after that - it does not continue with the next question. It just shows my background images with buttons. How can I set a code, that it goes straight to a new question, when the result image is gone.
I will be very thankful for your help.
I just want that after the Result-image closes, the quiz continues with the next question.
This is the code i have setup in Game.m file.
-(void)RightAnswer{
ScoreNumber = ScoreNumber + 1;
Score.text = [NSString stringWithFormat:#"%i", ScoreNumber];
Answer1.hidden = YES;
Answer2.hidden = YES;
Answer3.hidden = YES;
CategorySelected.hidden = NO;
Next.hidden = NO;
imageQuestion.hidden = YES;
Results.hidden = NO;
Results.image = [UIImage imageNamed:#"right.png"]; [self performSelector:#selector(Results) withObject:nil afterDelay:2.0];
GameInProgress = YES;
}
-(void)WrongAnswer{
LivesNumber = LivesNumber - 1;
Lives.text = [NSString stringWithFormat:#"%i", LivesNumber];
Answer1.hidden = YES;
Answer2.hidden = YES;
Answer3.hidden = YES;
imageQuestion.hidden = YES;
CategorySelected.hidden = NO;
Next.hidden = NO;
Results.hidden = NO;
Results.image = [UIImage imageNamed:#"wrong.png"];[self performSelector:#selector(Results) withObject:nil afterDelay:2.0];
GameInProgress = YES;
if(LivesNumber ==0) {
Results.image = [UIImage imageNamed:#"gameover.png"];
GameInProgress = NO;
Exit.hidden = NO;
}

-(void)RightAnswer{
ScoreNumber = ScoreNumber + 1;
Score.text = [NSString stringWithFormat:#"%i", ScoreNumber];
Answer1.hidden = YES;
Answer2.hidden = YES;
Answer3.hidden = YES;
Answer1Correct ==YES;
CategorySelected.hidden = NO;
Next.hidden = NO;
imageQuestion.hidden = YES;
Results.hidden = NO;
Results.image = [UIImage imageNamed:#"right.png"]; [self performSelector:#selector(Results) withObject:nil afterDelay:2.0];
GameInProgress = YES;
}
I think the issue is that you never set Answer1Correct to anything. so if you add Answer1Correct ==YES to the correct answer method and Answer1Correct ==NO to the wrong method then it should work

-(void)Results{ // just change formate of answer.
Results.hidden = YES;
}
-(IBAction)Answer1: (id) sender{
if (Answer1Correct ==YES) {
[self RightAnswer];
}else{
[self WrongAnswer];
}
}
-(IBAction)Answer2: (id) sender{
if (Answer2Correct ==YES) {
[self RightAnswer];
}
else{
[self WrongAnswer];
}
}
-(IBAction)Answer3: (id) sender{
if (Answer3Correct ==YES) {
[self RightAnswer];
}
else{
[self WrongAnswer];
}
}

i may be wrong here but I think you need an if to the else.
But I could be wrong.
if(LivesNumber ==0) {
Results.image = [UIImage imageNamed:#"gameover.png"];
GameInProgress = NO;
Exit.hidden = NO;
}else{
donextcard();
}
if you want to know how to make that function we would need to see more of the code.

Related

uiimageview.image not always changing when game over

Hi iam really new to coding and to start of i am making a really simple game in Xcode. The game is done but i do have a little problem. rarely at times when in game over pig.image does not change and the previous animation keeps on going even after [pig stopAnimation]... heres the code from when you get game over to where the image should change.....
-(void)gameover1{
[jetpacksound stop];
[self hitsound];
[pig stopAnimating];
[movementtimer invalidate];
if (score > highscorenumber) {
highscorenumber = score;
[[NSUserDefaults standardUserDefaults] setInteger:highscorenumber forKey:#"highscoresaved"];
}
else{
highscore.text = [NSString stringWithFormat:#"Try Harder Next Time"];
}
movementtimer = [NSTimer scheduledTimerWithTimeInterval:.035 target:self selector:#selector(gameover1movement) userInfo:nil repeats:YES];
bam.hidden = NO;
flash.hidden = NO;
background1.hidden = YES;
background2.hidden = YES;
background3.hidden = YES;
pauseg.hidden = YES;
scorelabel.hidden = YES;
fork1.hidden = YES;
fork2.hidden = YES;
fork3.hidden = YES;
fork4.hidden = YES;
knife1.hidden = YES;
knife2.hidden = YES;
knife3.hidden = YES;
knife4.hidden = YES;
poisoncloud.hidden = YES;
cake.hidden = YES;
}
-(void)gameover1movement{
pig.center = CGPointMake(pig.center.x, pig.center.y - 30);
if (IS_IPAD)
{
//do stuff for iPad
if (pig.center.y < 400) {
[self gameover2];
}
}
else
{
if(IS_IPHONE_5)
{
//do stuff for 4 inch iPhone screen
if (pig.center.y < 200) {
[self gameover2];
}
}
else
{
//do stuff for 3.5 inch iPhone screen
}
}
}
-(void)gameover2{
[movementtimer invalidate];
movementtimer = [NSTimer scheduledTimerWithTimeInterval:.035 target:self selector:#selector(gameover2movement) userInfo:nil repeats:YES];
pig.image = [UIImage imageNamed:#"DeadPig.png"];
background1.hidden = NO;
background2.hidden = NO;
background3.hidden = NO;
bam.hidden = YES;
flash.hidden = YES;
if (IS_IPAD)
{
//do stuff for iPad
pig.frame = CGRectMake(pig.center.x - 115, pig.center.y - 165, 230.0f, 330.0f);
if (pig.center.x < 115) {
pig.center = CGPointMake(115, pig.center.y);
}
if (pig.center.x > 653) {
pig.center = CGPointMake(653, pig.center.y);
}
background3.center = CGPointMake(384, 512);
background2.center = CGPointMake(384, 1536);
background1.center = CGPointMake(384, 2560);
}
else
{
if(IS_IPHONE_5)
{
//do stuff for 4 inch iPhone screen
pig.frame = CGRectMake(pig.center.x - 50, pig.center.y - 75, 100.0f, 150.0f);
if (pig.center.x < 50) {
pig.center = CGPointMake(50, pig.center.y);
}
if (pig.center.x > 270) {
pig.center = CGPointMake(270, pig.center.y);
}
background3.center = CGPointMake(160, 284);
background2.center = CGPointMake(160, 852);
background1.center = CGPointMake(160, 1420);
}
else
{
//do stuff for 3.5 inch iPhone screen
}
}
}

I am switching views in IOS 7 for a game and cannot get this code to work. Any suggestions?

Here is the Code I am currently using. If there is anything you need clarified I will do it as soon as I can. I have asked questions about other things as well that seemed to broad so I put every piece of code I am using related to the switch of views minus the basic stuff such as viewDidLoad.
Please read the following and answer/ comment as you see fit.
Thank you for the help.
-(IBAction)startgame:(id)sender
{
// load the game screen
if(self.gamescreenViewController == nil)
{
GameScreen *gameController = [[GameScreen alloc] initWithNibName:#"GameScreen" bundle:nil];
self.gamescreenViewController = gameController;
[gameController release];
}
// code to animate the switch
[UIView beginAnimations:#"View Flip" context:nil];
[UIView setAnimationDuration:1.25];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
// load Ultimate tic tac toe view
if(self.ultimateViewController.view.superview == nil)
{
// second code to animate switch
[UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self.view cache:YES];
// [UIView setAnimationTransition: UIViewAnimationOptionTransitionCurlDown forView:self.view cache:YES];
// show the ultimate view controller
[ultimateViewController viewWillAppear:YES];
[gamescreenViewController viewWillDisappear:YES];
[gamescreenViewController.view removeFromSuperview];
[self.view insertSubview:ultimateViewController.view atIndex:0];
[gamescreenViewController viewDidDisappear:YES];
[ultimateViewController viewDidAppear:YES];
// change button image
UIImage *buttonImage = [UIImage imageNamed:#"StartGame.png"];
[switchbutton setBackgroundImage:buttonImage forState:UIControlStateNormal];
[self.view addSubview:switchbutton];
// setting values to zero after switch buttton action
int symb1 = [[self.ultimateViewController.Symbolfield1 text] intValue];
int symb2 = [[self.ultimateViewController.Symbolfield2 text] intValue];
int symb3 = [[self.ultimateViewController.Symbolfield3 text] intValue];
int turns = [[self.gamescreenViewController.turns text] intValue];
symb1 = 0;
symb2 = 0;
symb3 = 0;
turns = 1;
[ultimateViewController.Symbolfield1 setText:[NSString stringWithFormat:#"%1i", symb1]];
[ultimateViewController.Symbolfield2 setText:[NSString stringWithFormat:#"%1i", symb2]];
[ultimateViewController.Symbolfield3 setText:[NSString stringWithFormat:#"%1i", symb3]];
[gamescreenViewController.turns setText:[NSString stringWithFormat:#"%1i", turns]];
// endable buttons after switch button
ultimateViewController.a1.enabled = YES;
ultimateViewController.a2.enabled = YES;
ultimateViewController.a3.enabled = YES;
ultimateViewController.a4.enabled = YES;
ultimateViewController.a5.enabled = YES;
ultimateViewController.a6.enabled = YES;
ultimateViewController.a7.enabled = YES;
ultimateViewController.a8.enabled = YES;
ultimateViewController.a9.enabled = YES;
ultimateViewController.a10.enabled = YES;
ultimateViewController.a11.enabled = YES;
ultimateViewController.a12.enabled = YES;
ultimateViewController.b1.enabled = YES;
ultimateViewController.b2.enabled = YES;
ultimateViewController.b3.enabled = YES;
ultimateViewController.b4.enabled = YES;
ultimateViewController.b5.enabled = YES;
ultimateViewController.b6.enabled = YES;
ultimateViewController.b7.enabled = YES;
ultimateViewController.b8.enabled = YES;
ultimateViewController.b9.enabled = YES;
ultimateViewController.b10.enabled = YES;
ultimateViewController.b11.enabled = YES;
ultimateViewController.b12.enabled = YES;
ultimateViewController.c1.enabled = YES;
ultimateViewController.c2.enabled = YES;
ultimateViewController.c3.enabled = YES;
ultimateViewController.c4.enabled = YES;
ultimateViewController.c5.enabled = YES;
ultimateViewController.c6.enabled = YES;
ultimateViewController.c7.enabled = YES;
ultimateViewController.c8.enabled = YES;
ultimateViewController.c9.enabled = YES;
ultimateViewController.c10.enabled = YES;
ultimateViewController.c11.enabled = YES;
ultimateViewController.c12.enabled = YES;
gamescreenViewController.move1.enabled = YES;
gamescreenViewController.move2.enabled = YES;
gamescreenViewController.move3.enabled = YES;
gamescreenViewController.move4.enabled = YES;
gamescreenViewController.move5.enabled = YES;
gamescreenViewController.move6.enabled = YES;
gamescreenViewController.move7.enabled = YES;
gamescreenViewController.move8.enabled = YES;
gamescreenViewController.move9.enabled = YES;
gamescreenViewController.move10.enabled = YES;
gamescreenViewController.move11.enabled = YES;
gamescreenViewController.move12.enabled = YES;
gamescreenViewController.move13.enabled = YES;
gamescreenViewController.move14.enabled = YES;
gamescreenViewController.move15.enabled = YES;
gamescreenViewController.move16.enabled = YES;
gamescreenViewController.move17.enabled = YES;
gamescreenViewController.move18.enabled = YES;
gamescreenViewController.move19.enabled = YES;
gamescreenViewController.move20.enabled = YES;
gamescreenViewController.move21.enabled = YES;
gamescreenViewController.move22.enabled = YES;
gamescreenViewController.move23.enabled = YES;
gamescreenViewController.move24.enabled = YES;
gamescreenViewController.move25.enabled = YES;
gamescreenViewController.move26.enabled = YES;
gamescreenViewController.move27.enabled = YES;
gamescreenViewController.move28.enabled = YES;
gamescreenViewController.move29.enabled = YES;
gamescreenViewController.move30.enabled = YES;
gamescreenViewController.move31.enabled = YES;
gamescreenViewController.move32.enabled = YES;
gamescreenViewController.move33.enabled = YES;
gamescreenViewController.move34.enabled = YES;
gamescreenViewController.move35.enabled = YES;
gamescreenViewController.move36.enabled = YES;
gamescreenViewController.move37.enabled = YES;
gamescreenViewController.move38.enabled = YES;
gamescreenViewController.move39.enabled = YES;
gamescreenViewController.move40.enabled = YES;
gamescreenViewController.move41.enabled = YES;
gamescreenViewController.move42.enabled = YES;
gamescreenViewController.move43.enabled = YES;
gamescreenViewController.move44.enabled = YES;
gamescreenViewController.move45.enabled = YES;
gamescreenViewController.move46.enabled = YES;
gamescreenViewController.move47.enabled = YES;
gamescreenViewController.move48.enabled = YES;
gamescreenViewController.move49.enabled = YES;
gamescreenViewController.move50.enabled = YES;
gamescreenViewController.move51.enabled = YES;
gamescreenViewController.move52.enabled = YES;
gamescreenViewController.move53.enabled = YES;
gamescreenViewController.move54.enabled = YES;
gamescreenViewController.move55.enabled = YES;
gamescreenViewController.move56.enabled = YES;
gamescreenViewController.move57.enabled = YES;
gamescreenViewController.move58.enabled = YES;
gamescreenViewController.move59.enabled = YES;
gamescreenViewController.move60.enabled = YES;
gamescreenViewController.move61.enabled = YES;
gamescreenViewController.move62.enabled = YES;
gamescreenViewController.move63.enabled = YES;
gamescreenViewController.move64.enabled = YES;
gamescreenViewController.move65.enabled = YES;
gamescreenViewController.move66.enabled = YES;
gamescreenViewController.move67.enabled = YES;
gamescreenViewController.move68.enabled = YES;
gamescreenViewController.move69.enabled = YES;
gamescreenViewController.move70.enabled = YES;
gamescreenViewController.move71.enabled = YES;
gamescreenViewController.move72.enabled = YES;
gamescreenViewController.move73.enabled = YES;
gamescreenViewController.move74.enabled = YES;
gamescreenViewController.move75.enabled = YES;
gamescreenViewController.move76.enabled = YES;
gamescreenViewController.move77.enabled = YES;
gamescreenViewController.move78.enabled = YES;
gamescreenViewController.move79.enabled = YES;
gamescreenViewController.move80.enabled = YES;
gamescreenViewController.move81.enabled = YES;
// clear images after switch button action
gamescreenViewController.image1a.image = NO;
gamescreenViewController.image2a.image = NO;
gamescreenViewController.image3a.image = NO;
gamescreenViewController.image4a.image = NO;
gamescreenViewController.image5a.image = NO;
gamescreenViewController.image6a.image = NO;
gamescreenViewController.image7a.image = NO;
gamescreenViewController.image8a.image = NO;
gamescreenViewController.image9a.image = NO;
gamescreenViewController.image1b.image = NO;
gamescreenViewController.image2b.image = NO;
gamescreenViewController.image3b.image = NO;
gamescreenViewController.image4b.image = NO;
gamescreenViewController.image5b.image = NO;
gamescreenViewController.image6b.image = NO;
gamescreenViewController.image7b.image = NO;
gamescreenViewController.image8b.image = NO;
gamescreenViewController.image9b.image = NO;
gamescreenViewController.image1c.image = NO;
gamescreenViewController.image2c.image = NO;
gamescreenViewController.image3c.image = NO;
gamescreenViewController.image4c.image = NO;
gamescreenViewController.image5c.image = NO;
gamescreenViewController.image6c.image = NO;
gamescreenViewController.image7c.image = NO;
gamescreenViewController.image8c.image = NO;
gamescreenViewController.image9c.image = NO;
gamescreenViewController.image1d.image = NO;
gamescreenViewController.image2d.image = NO;
gamescreenViewController.image3d.image = NO;
gamescreenViewController.image4d.image = NO;
gamescreenViewController.image5d.image = NO;
gamescreenViewController.image6d.image = NO;
gamescreenViewController.image7d.image = NO;
gamescreenViewController.image8d.image = NO;
gamescreenViewController.image9d.image = NO;
gamescreenViewController.image1e.image = NO;
gamescreenViewController.image2e.image = NO;
gamescreenViewController.image3e.image = NO;
gamescreenViewController.image4e.image = NO;
gamescreenViewController.image5e.image = NO;
gamescreenViewController.image6e.image = NO;
gamescreenViewController.image7e.image = NO;
gamescreenViewController.image8e.image = NO;
gamescreenViewController.image9e.image = NO;
gamescreenViewController.image1f.image = NO;
gamescreenViewController.image2f.image = NO;
gamescreenViewController.image3f.image = NO;
gamescreenViewController.image4f.image = NO;
gamescreenViewController.image5f.image = NO;
gamescreenViewController.image6f.image = NO;
gamescreenViewController.image7f.image = NO;
gamescreenViewController.image8f.image = NO;
gamescreenViewController.image9f.image = NO;
gamescreenViewController.image1g.image = NO;
gamescreenViewController.image2g.image = NO;
gamescreenViewController.image3g.image = NO;
gamescreenViewController.image4g.image = NO;
gamescreenViewController.image5g.image = NO;
gamescreenViewController.image6g.image = NO;
gamescreenViewController.image7g.image = NO;
gamescreenViewController.image8g.image = NO;
gamescreenViewController.image9g.image = NO;
gamescreenViewController.image1h.image = NO;
gamescreenViewController.image2h.image = NO;
gamescreenViewController.image3h.image = NO;
gamescreenViewController.image4h.image = NO;
gamescreenViewController.image5h.image = NO;
gamescreenViewController.image6h.image = NO;
gamescreenViewController.image7h.image = NO;
gamescreenViewController.image8h.image = NO;
gamescreenViewController.image9h.image = NO;
gamescreenViewController.image1i.image = NO;
gamescreenViewController.image2i.image = NO;
gamescreenViewController.image3i.image = NO;
gamescreenViewController.image4i.image = NO;
gamescreenViewController.image5i.image = NO;
gamescreenViewController.image6i.image = NO;
gamescreenViewController.image7i.image = NO;
gamescreenViewController.image8i.image = NO;
gamescreenViewController.image9i.image = NO;
gamescreenViewController.playerturnimage.image = NO;
gamescreenViewController.playerturnlabel.text = #"Your Turn Player One";
}
else
{
// load game screen view
if(self.gamescreenViewController.view.superview == nil)
{
int symb1 = [[self.ultimateViewController.Symbolfield1 text] intValue];
int symb2 = [[self.ultimateViewController.Symbolfield2 text] intValue];
int symb3 = [[self.ultimateViewController.Symbolfield3 text] intValue];
if ((symb1 < 1) || (symb2 <1) || (symb3 <1))
{
// warning message
UIAlertView *alertview1 = [[ UIAlertView alloc]
initWithTitle:#"No Symbols Selected"
message:#"\n You must select an \n image for each player \n\n"
delegate:nil cancelButtonTitle:#"Got it"
otherButtonTitles: nil];
[alertview1 show];
[alertview1 release];
}
else
{
// show the game screen
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view cache:YES];
// show the game controller
[gamescreenViewController viewWillAppear:YES];
[ultimateViewController viewWillDisappear:YES];
[ultimateViewController.view removeFromSuperview];
[self.view insertSubview:gamescreenViewController.view atIndex:0];
[ultimateViewController viewDidDisappear:YES];
[gamescreenViewController viewDidAppear:YES];
// change button image
UIImage *buttonImage = [UIImage imageNamed:#"EndGame.png"];
[switchbutton setBackgroundImage:buttonImage forState:UIControlStateNormal];
[self.view addSubview:switchbutton];
// hides and disables the undo button for the first move
gamescreenViewController.undobutton.enabled = NO;
gamescreenViewController.undobutton.hidden = YES;
}
// send values from ultimate viewcontroller to gamescreen
int playerpick = [[self.ultimateViewController.SegmentChange text] intValue];
[self.gamescreenViewController.SegmentChange2a setText:[NSString stringWithFormat:#"%1i",playerpick]];
int symbol1 = [[self.ultimateViewController.Symbolfield1 text] intValue];
[self.gamescreenViewController.Symbolfield1a setText:[NSString stringWithFormat:#"%1i",symbol1]];
int symbol2 = [[self.ultimateViewController.Symbolfield2 text] intValue];
[self.gamescreenViewController.Symbolfield2a setText:[NSString stringWithFormat:#"%1i",symbol2]];
int symbol3 = [[self.ultimateViewController.Symbolfield3 text] intValue];
[self.gamescreenViewController.Symbolfield3a setText:[NSString stringWithFormat:#"%1i",symbol3]];
}
}
// statement to enact switch animation
[UIView commitAnimations];
}
Keep in mind that this is set to a certain game I am making as an action connected to the start button.
Any suggestions?
It looks like you aren't actually presenting the view that you are loading from the nib.

Expected identifier on if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)

*This is the whole code :)
I have been trying to fix this for an hour now, but i can still not make it.
I would be happy if someone could help me :)
Still can't make the UI_USER_INTERFACE_IDIOM code work.*
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
-(void)Collision{
if (CGRectIntersectsRect(Heli.frame, Obstacle.frame)) {
[self EndGame];
}
if (CGRectIntersectsRect(Heli.frame, Obstacle2.frame)) {
[self EndGame];
}
if (CGRectIntersectsRect(Heli.frame, Bottom1.frame)) {
[self EndGame];
}
if (CGRectIntersectsRect(Heli.frame, Top1.frame)) {
[self EndGame];
}
}
-(void)EndGame{
if (Scorenumber > HighScore) {
HighScore = Scorenumber;
[[NSUserDefaults standardUserDefaults] setInteger:HighScore
forKey:#"HighScoreSaved"];
}
Heli.hidden = YES;
[timer invalidate];
[Scorer invalidate];
[self performSelector:#selector(NewGame) withObject: nil afterDelay:2];
}
-(void)NewGame{
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
// For iPhone
Bottom1.hidden = YES;
Top1.hidden = YES;
Obstacle.hidden = YES;
Obstacle2.hidden = YES;
corona.hidden = YES;
Intro1.hidden = NO;
Intro2.hidden = NO;
Intro3.hidden = NO;
Heli.hidden = NO;
Heli.center = CGPointMake(88, 286);
Heli.image = [UIImage imageNamed:#"buss til app opp.png"];
Start = YES;
Scorenumber = 0;
Score.text = [NSString stringWithFormat:#"Score: 0"];
Intro3.text = [NSString stringWithFormat:#"HighScore: %i", HighScore];
}
} else{
// For iPad
Bottom1.hidden = YES;
Top1.hidden = YES;
Obstacle.hidden = YES;
Obstacle2.hidden = YES;
corona.hidden = YES;
Intro1.hidden = NO;
Intro2.hidden = NO;
Intro3.hidden = NO;
Heli.hidden = NO;
Heli.center = CGPointMake(153, 515);
Heli.image = [UIImage imageNamed:#"buss til app opp.png"];
Start = YES;
Scorenumber = 0;
Score.text = [NSString stringWithFormat:#"Score: 0"];
Intro3.text = [NSString stringWithFormat:#"HighScore: %i", HighScore];
Just look at following code:
-(void)NewGame{
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
// For iPhone
Bottom1.hidden = YES;
Top1.hidden = YES;
Obstacle.hidden = YES;
Obstacle2.hidden = YES;
corona.hidden = YES;
Intro1.hidden = NO;
Intro2.hidden = NO;
Intro3.hidden = NO;
Heli.hidden = NO;
Heli.center = CGPointMake(88, 286);
Heli.image = [UIImage imageNamed:#"buss til app opp.png"];
Start = YES;
Scorenumber = 0;
Score.text = [NSString stringWithFormat:#"Score: 0"];
Intro3.text = [NSString stringWithFormat:#"HighScore: %i", HighScore];
}
} else{
and see - "}" symbol on line before "} else{" is wrong. It is the pair for
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
opening construction, so "else" is "standalone - it is wrong.
Try to delete this "}".

Facing issue with UISwitch on/off

There are 6 switch controls in the screen. At one time only one switch should be enable.If 5th switch is on then, one label and one text field should be visible either it should be hidden.
I am facing issue, when 5 switch is changed to on from off and again changed to off. The label and text filed should be hidden but it's not working as per requirement.
- (void)viewDidLoad
{
[super viewDidLoad];
self.sw_switch1.on = NO;
self.sw_switch2.on = NO;
self.sw_switch3.on = NO;
self.sw_switch4.on = NO;
self.sw_switch5.on = NO;
self.sw_switch6.on = NO;
lbl_desc.hidden = YES;
txt_desc.hidden = YES;
}
-(IBAction)switchChange:(id)sender
{
if(sw_switch1.isOn)
{
//self.sw_switch1.on = NO;
self.sw_switch2.on = NO;
self.sw_switch3.on = NO;
self.sw_switch4.on = NO;
self.sw_switch5.on = NO;
self.sw_switch6.on = NO;
}
else if(sw_switch2.isOn)
{
self.sw_switch1.on = NO;
//self.sw_switch2.on = NO;
self.sw_switch3.on = NO;
self.sw_switch4.on = NO;
self.sw_switch5.on = NO;
self.sw_switch6.on = NO;
}
else if(sw_switch3.isOn)
{
self.sw_switch1.on = NO;
self.sw_switch2.on = NO;
//self.sw_switch3.on = NO;
self.sw_switch4.on = NO;
self.sw_switch5.on = NO;
self.sw_switch6.on = NO;
}
else if(sw_switch4.isOn)
{
self.sw_switch1.on = NO;
self.sw_switch2.on = NO;
self.sw_switch3.on = NO;
//self.sw_switch4.on = NO;
self.sw_switch5.on = NO;
self.sw_switch6.on = NO;
}
else if(sw_switch5.isOn)
{
self.sw_switch1.on = NO;
self.sw_switch2.on = NO;
self.sw_switch3.on = NO;
self.sw_switch4.on = NO;
//self.sw_switch5.on = NO;
self.sw_switch6.on = NO;
lbl_desc.hidden = YES;
txt_desc.hidden = YES;
}
else if(sw_switch6.isOn)
{
lbl_desc.hidden = NO;
txt_desc.hidden = NO;
self.sw_switch1.on = NO;
self.sw_switch2.on = NO;
self.sw_switch3.on = NO;
self.sw_switch4.on = NO;
self.sw_switch5.on = NO;
//self.sw_switch6.on = NO;
}
[self viewWillAppear:NO];
}
You hide the labels (.hidden = YES) when the switched is swithed to on. But you do not unhide (.hidden=NO) when the others are switched off. Your code is a bit unlogical anyway.
I suggest:
-(IBAction)switchChange:(id)sender
{
UISwitch *theSwitch = (UISwitch *) sender;
BOOL switchState = theSwitch.on; //this method is called when changed to on or off.
self.sw_switch1.on = NO; // force all switches off
self.sw_switch2.on = NO;
self.sw_switch3.on = NO;
self.sw_switch4.on = NO;
self.sw_switch5.on = NO;
self.sw_switch6.on = NO;
theSwitch.on = switchState; // recover the state set by the user.
//now hide or unhide the labels depending on the state of switch 5.
lbl_desc.hidden = self.sw_switch5.on;
txt_desc.hidden = self.sw_switch5.on;
// [self viewWillAppear:NO]; // What the h.... is this? You are not supposed to call that method. Use self.view.setNeedsDisplay or so instead.
[self.view setNeedsDisplay]; // This may not be reqired but it forces the view to re-display itself.
}
You can try this
if(sw_switch5.isOn)
{
self.sw_switch1.on = NO;
self.sw_switch2.on = NO;
self.sw_switch3.on = NO;
self.sw_switch4.on = NO;
//self.sw_switch5.on = NO;
self.sw_switch6.on = NO;
lbl_desc.hidden = NO;
txt_desc.hidden = NO;
}
else
{
lbl_desc.hidden = YES;
txt_desc.hidden = YES;
}
My suggestion is you should use tag value also for each switch control.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
sw_switch1.tag = 101;
sw_switch2.tag = 102;
sw_switch3.tag = 103;
sw_switch4.tag = 104;
sw_switch5.tag = 105;
sw_switch6.tag = 106;
self.sw_switch1.on = NO;
self.sw_switch2.on = NO;
self.sw_switch3.on = NO;
self.sw_switch4.on = NO;
self.sw_switch5.on = NO;
self.sw_switch6.on = NO;
lbl_desc.hidden = YES;
txt_desc.hidden = YES;
}
Edit: Use the tag value like this if((theSwitch.tag == 105) && self.sw_switch5.on).
Now you can the try the below optimized code
-(IBAction)switchChange:(id)sender
{
UISwitch *theSwitch = (UISwitch *) sender;
BOOL switchState = theSwitch.on; //this method is called when changed to on or off.
self.sw_switch1.on = NO; // force all switches off
self.sw_switch2.on = NO;
self.sw_switch3.on = NO;
self.sw_switch4.on = NO;
self.sw_switch5.on = NO;
self.sw_switch6.on = NO;
lbl_desc.hidden = YES;// force the label and textfield should be hidden
txt_desc.hidden = YES;
theSwitch.on = switchState; // recover the state set by the user, so at a time only one switch should be on
//now hide or unhide the labels depending on the state of switch 5 only
if((theSwitch.tag == 105) && self.sw_switch5.on){
lbl_desc.hidden = NO;
txt_desc.hidden = NO;
}
else{
lbl_desc.hidden = YES;
txt_desc.hidden = YES;
}
[self.view setNeedsDisplay];
}

How do I use a for loop to display NSArray of UIImage's

Hey guys,
So.... lets say I have an NSArray of images
NSMutableArray *images = [NSMutableArray new];
[images addObject:[UIImage imageNamed:#"line1.png"]];
[images addObject:[UIImage imageNamed:#"line2.png"]];
[images addObject:[UIImage imageNamed:#"line3.png"]];
[images addObject:[UIImage imageNamed:#"line4.png"]];
Now I would like to load all these at once using a for loop but here is the catch.... I need to be able to set the images as hidden until the user unhides through interaction.
for (UIImage *image in images) {
UIImageView *line = [[UIImageView alloc] initWithImage:image];
line.hidden = YES;
[self.view addSubview:line];
}
But then how to I set the hidden BOOL to NO using another method?
As a secondary question, how would one release *line in the code above?
Thanks,
Darren
One option is to set up your images like:
int nextTag = 1;
for (UIImage *image in images) {
UIImageView *line = [[UIImageView alloc] initWithImage:image];
line.hidden = YES;
line.tag = nextTag;
[self.view addSubview:line];
[line release];
nextTag++;
}
...and then to unhide them you can do:
UIView* imageView = [self.view viewWithTag: lineNumber];
imageView.hidden = NO;
...assuming that your user-interaction handler is able to determine what line in the UI the user is interacting with.
As a secondary question, how would one release *line in the code above?
for (UIImage *image in images) {
UIImageView *line = [[UIImageView alloc] initWithImage:image];
line.hidden = YES;
[self.view addSubview:line]; // this retains the subview.
[line release]; // release line like this.
}
**
-(IBAction)btnReviewStar:(id)sender{
for (int i =([sender tag] ==30); i<36; i++) {
btnReviewStar.selected = NO;
btnReviewStar1.selected = NO;
btnReviewStar2.selected = NO;
btnReviewStar3.selected = NO;
btnReviewStar4.selected = NO;
if([sender tag] == 31) {
btnReviewStar.selected = YES;
break;
} else if([sender tag]==32) {
btnReviewStar.selected = YES;
btnReviewStar1.selected = YES;
break;
} else if([sender tag]==33) {
btnReviewStar.selected = YES;
btnReviewStar1.selected = YES;
btnReviewStar2.selected = YES;
break;
} else if([sender tag]==34) {
btnReviewStar.selected = YES;
btnReviewStar1.selected = YES;
btnReviewStar2.selected = YES;
btnReviewStar3.selected = YES;
break;
} else {
btnReviewStar.selected = YES;
btnReviewStar1.selected = YES;
btnReviewStar2.selected = YES;
btnReviewStar3.selected = YES;
btnReviewStar4.selected = YES;
break;
}
}
}
**

Resources