Need Help Linking Multiple Labels to same IBOutlet

Discussion in 'iOS Programming' started by simplymuzik3, Mar 25, 2009.

  1. simplymuzik3 macrumors 6502a

    Jan 29, 2009
    Okay, so here's my problem. I have a game that im currently developing and Im adding a basic life system. The only problem Im having, is that there is a label to display the life, and that works fine. But when I go to a new view(happens frequently in my game), I need to connect the label in my new view to the same IBOutlet as my initial label, so that it will remain updated with the life value. Can someone please show me how to do this, I need to link the label in the 2nd, 3rd, 4th views etc, to the same IBOutlet as the label in the 1st view. I dont want to make about 50 IBOutlets for a new label on each view.

    Also, when I preform a baisc check for the life Im having some problems. I have it setup so that if the life < 0, it does the following:

    [self removeFromSuperview];
    [self addSubview:GameOver];
    This works fine when I want to transition between views with buttons(I replace the first "self" with the current view name), but when I do it like this by adding the "self removeFromSuperview];" I get problems. Instead of going to the GameOver view, it just goes to a plain white view(which I dont have). I can get it to work with buttons, but I keep having problems when I try to make it automatically go to the GameOver view when the life <0.

    I hope someone can help me with this, and please dont be harsh. I just started programming for iPhone, so I would REALLY appreciate the help. Thanks for reading my long post! :D

    EDIT: I got the second problem to work with the views not coming up correctly. I just removed the first line of code"[self removeFromSuperview];" and it worked perfectly. I know, that this is eating up processes/cpu cycles because the view was not released, but instead a new view was loaded on top of it. If anyone knows a better way that works, Im happy to take suggestions!
  2. iphonedevelop18 macrumors member

    Feb 26, 2009
    for your first problem thats really easy, just connect it basically and then it will say its connected to multiple outlets, therefor changing every label every time you change the .text
  3. simplymuzik3 thread starter macrumors 6502a

    Jan 29, 2009
    Okay, for the first problem, a completely new view is loaded many times throughout the game. Each view starts off as a blank view and I completely redesign each view. I have to insert a new label in Each view(for the life) because the previous label was removed, along with the view it was part of. I think I might have another way to do it. Can you make the view background transparent, so that you can still interact with the view that is visible under the transparent one? I could just add a new view and put the label on it, and just set the Background to transparent to the user can still interact with the views below it. Will this work? Can you interact with the view below the transparent one(transparent on top, and fully visible on the bottom)? Any more help is appreciated! Thanks! :)
  4. iphonedevelop18 macrumors member

    Feb 26, 2009
    I would just create all my views and then have back button on each one so that when you click the back button it removes the current view and adds the view that you were at previously
    Like this
    if you have multiple view controllers, set a back button on each one and create IBActions for them. Then label the actions back1,black2 etc.
    put this in your .m view controllers
         [self removeFromSuperview];
         [window addSubview:firstView];
    Changing that with each view controller and making back2 to go to secondView.

    Hope that helps as this is the way I would tackle the problem
  5. simplymuzik3 thread starter macrumors 6502a

    Jan 29, 2009

    thanks for the help! I used your method and it worked perfectly! Now I just need to link my multiple labels to the same IBOutlet! Does anyone have any answers for this, check my posts above for more info on my problem!
  6. iphonedevelop18 macrumors member

    Feb 26, 2009

    If you used my method with the multiple view controllers then you will have to import the main (first) view that declares the UILabel, like so
    #import "firstView.h"
    at the top of all of your .m view controllers.
    By doing this you will have access to that label in your other view, and if I'm not mistaken, it will then show up in your connections tab then you just connect to the label you want.

    If that doesn't work I guess you would have to declare a new UILabel under every view controller.

    Hope this helps!

    P.S. I am also just beginning iphone app development so these solutions may not be the best way of tackling the problem however, they will, hopefully, work

Share This Page