Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID
Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.

How to use a count up timer in many UIViewControllers?

Kurt88Kurt88 Posts: 4New Users
Making a quiz, with more than 100 UIViewControllers. I want to use a count up timer, that will continue in every UIViewController, from question 1 to the last one. So far, I managed to count up only in the first ViewController.
I wrote:


in h.

{
float mainInt;
IBOutlet UILabel *label;
NSTimer *randomMain;
}

in m.

- (void)viewDidLoad
{


mainInt=0;
randomMain=[NSTimer scheduledTimerWithTimeInterval:0.1f target:self selector:@selector(randomMainVoid) userInfo:nil repeats:YES];


[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
-(void)randomMainVoid{
mainInt+=0.1;
label.text=[NSString stringWithFormat:@"%.01f", mainInt];

}
Post edited by Kurt88 on

Replies

  • SpeedSpeed Posts: 614Registered Users
    more than 100 UIViewControllers
    I would presume that you have one view controller for each question and answer. Have you thought about having one view controller to handle the question and answer and just pass that controller the relevant data that it would need to display the questions, answers, and ultimate result, thus reducing your controller count from 100 to one?
    I want to use a count up timer, that will continue in every UIViewController, from question 1 to the last one.
    I would place the timer in a singleton rather than in each view controller, thus making it run even when the view controller is no longer presented. Or, you could have a globally accessible integer available and increment that using your timer that is available in each view controller.
  • Duncan CDuncan C Posts: 8,025Tutorial Authors, Registered Users
    Kurt88;433553 said:
    Making a quiz, with more than 100 UIViewControllers. I want to use a count up timer, that will continue in every UIViewController, from question 1 to the last one. So far, I managed to count up only in the first ViewController.
    I wrote:


    in h.

    {
    float mainInt;
    IBOutlet UILabel *label;
    NSTimer *randomMain;
    }

    in m.

    - (void)viewDidLoad
    {


    mainInt=0;
    randomMain=[NSTimer scheduledTimerWithTimeInterval:0.1f target:self selector:@selector(randomMainVoid) userInfo:nil repeats:YES];


    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    }
    -(void)randomMainVoid{
    mainInt+=0.1;
    label.text=[NSString stringWithFormat:@"%.01f", mainInt];

    }
    Any time you have over 100 view controllers you are doing something wrong. Speed has the right idea. A view controller is like a blank paper form. You provide data that changes, and the view controller displays that data. You should not have a separate view controller for each bit of data you want to display.
    Regards,

    Duncan C
    WareTo

    mug

    Animated GIF created with Face Dancer, available for free in the app store.
  • Kurt88Kurt88 Posts: 4New Users
    Duncan C;433557 said:
    Any time you have over 100 view controllers you are doing something wrong. Speed has the right idea. A view controller is like a blank paper form. You provide data that changes, and the view controller displays that data. You should not have a separate view controller for each bit of data you want to display.
    I am planning to make a quiz like "impossible test" and "moron test". Every question will have a different background, graphics, clip art. You find the right answer and proceed to the next question, you lose and start from the beginning or the checkpoint. No score, only timer. I thought the easiest way was to make the whole image (with 4 answers and question) in photoshop and place it in ViewController. Will I a have a big problem with memory and size?
  • SpeedSpeed Posts: 614Registered Users
    Kurt88;433560 said:
    I am planning to make a quiz like "impossible test" and "moron test". Every question will have a different background, graphics, clip art. You find the right answer and proceed to the next question, you lose and start from the beginning or the checkpoint. No score, only timer. I thought the easiest way was to make the whole image (with 4 answers and question) in photoshop and place it in ViewController. Will I a have a big problem with memory and size?
    Having hundreds of images will definitely increase the size of your application, possibly past the 3G download limit. However, you shouldn't have an issue when running the application, due to the fact that only one of those images should be on the screen at one time.
  • Kurt88Kurt88 Posts: 4New Users
    Speed;433555 said:
    I would presume that you have one view controller for each question and answer. Have you thought about having one view controller to handle the question and answer and just pass that controller the relevant data that it would need to display the questions, answers, and ultimate result, thus reducing your controller count from 100 to one?



    I would place the timer in a singleton rather than in each view controller, thus making it run even when the view controller is no longer presented. Or, you could have a globally accessible integer available and increment that using your timer that is available in each view controller.
    Trying to make it globally accessible I wrote:

    AppDelegate .h file and q1ViewController .h file:




    @interface AppDelegate : UIResponder {
    float mainInt;

    NSTimer *randomMain;
    IBOutlet UILabel *label;

    }



    In q1ViewController .m file:





    - (void)viewDidLoad
    {


    MyAppDelegate *mainDelegate= (MyAppCelegate *) [[UIApplication sharedApplication]delegate];

    delegate.randomMain=0;

    mainInt=0;
    mainDelegate.randomMain=[NSTimer scheduledTimerWithTimeInterval:0.1f target:self selector:@selector(randomMainVoid) userInfo:nil repeats:YES];


    [super viewDidLoad];

    }
    -(void)randomMainVoid{
    mainInt+=0.1;
    label.text=[NSString stringWithFormat:@"%.01f", mainInt];

    }


    However it doesn't work. Is this the right way?
  • Duncan CDuncan C Posts: 8,025Tutorial Authors, Registered Users
    Kurt88;433726 said:
    Trying to make it globally accessible I wrote:

    AppDelegate .h file and q1ViewController .h file:




    @interface AppDelegate : UIResponder {
    float mainInt;

    NSTimer *randomMain;
    IBOutlet UILabel *label;

    }



    In q1ViewController .m file:





    - (void)viewDidLoad
    {


    MyAppDelegate *mainDelegate= (MyAppCelegate *) [[UIApplication sharedApplication]delegate];

    delegate.randomMain=0;

    mainInt=0;
    mainDelegate.randomMain=[NSTimer scheduledTimerWithTimeInterval:0.1f target:self selector:@selector(randomMainVoid) userInfo:nil repeats:YES];


    [super viewDidLoad];

    }
    -(void)randomMainVoid{
    mainInt+=0.1;
    label.text=[NSString stringWithFormat:@"%.01f", mainInt];

    }


    However it doesn't work. Is this the right way?
    No, it is very definitely NOT the right way.

    You should never try to access views directly from outside of the view controller that owns them.

    What I would suggest doing is creating a timer in your app delegate, and have that timer broadcast a "timer fired" notification. Then, in each view controller, add an observer for that notification in your viewWillAppear method, and remove the observer your viewWillDisappear method.

    Then, the front view controller will get notified about the timer firing, and other view controllers will not (which is what you want.)

    In the method for your notification, do whatever you need to do with the timer.
    Regards,

    Duncan C
    WareTo

    mug

    Animated GIF created with Face Dancer, available for free in the app store.
  • Kurt88Kurt88 Posts: 4New Users
    Duncan C;433737 said:
    No, it is very definitely NOT the right way.

    You should never try to access views directly from outside of the view controller that owns them.

    What I would suggest doing is creating a timer in your app delegate, and have that timer broadcast a "timer fired" notification. Then, in each view controller, add an observer for that notification in your viewWillAppear method, and remove the observer your viewWillDisappear method.

    Then, the front view controller will get notified about the timer firing, and other view controllers will not (which is what you want.)

    In the method for your notification, do whatever you need to do with the timer.
    I am really sorry, how I am going to broadcast a "timer fired" notification in app delegate?


    I wrote in app delegate .m:


    -(void)timer:(NSNotification *) notification{



    mainInt=0;
    timer=[NSTimer scheduledTimerWithTimeInterval:0.1f target:self selector:@selector(timer) userInfo:nil repeats:YES];

    mainInt+=0.1;
    label.text=[NSString stringWithFormat:@"%.01f", mainInt];


    }

    and then added and removed an observer in each view controller:

    -(void)viewWillAppear:(BOOL)animated{

    [super viewDidLoad];

    [[NSNotificationCenter defaultCenter] addObserver:self
    selector:@selector(refreshData)
    name:@"timer"
    object:nil];


    }

    -(void)viewWillDisappear:(BOOL)animated{

    [super viewDidDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self
    name:@"timer"
    object:nil];

    }
Sign In or Register to comment.