NSTimer on UIToolbar - xcode

In my project I am implementing NSTimer to calculate time.
What I want to do is,
I am having my UIToolBar in my Timer.m file with tag 100.
and I want to add NSTimer on my UIToolBar from TimerController.m
But I am not able to add NStimer on UIToolBar.
My Timer is getting added over my navigation bar.
How to add my Timer on UIToolbar.
Can any one guide me.
Thanks in advance.
Here is my code...
Timer.m
UIToolbar* bottomToolbar = [[UIToolbar alloc] init];
bottomToolbar.frame = CGRectMake(0, 390, self.frame.size.width, 44);
[bottomToolbar sizeToFit];
bottomToolbar.tintColor = [[UIColor blackColor] colorWithAlphaComponent:0.6];
bottomToolbar.tag = 100;
[self addSubview:bottomToolbar];
and this is my time in TimerController.h
NSTimer* timer;
}
#property (nonatomic,retain) NSTimer* timer;
and code for this timer in TimerController.m
timer=[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(displayTime) userInfo:nil repeats:YES];
and in displayTime method I will go for timing calculation.
I didn't found ant property to add timer over UIToolBar.
I tried using below code, but it give me warning with no output.
Warning as "incompatible pointer types sending NStimer* to prameter of UIView*"
UIToolbar *bottomBar = (UIToolbar*)[self.view viewWithTag:100];
[bottomBar addSubview:timer];
Here is the image how I want it....
#note :- I also checked whether my Toolbar tag is getting problem, but NO,
I am able to add UIToolbar and nslog tag correctly.
Thanks in advance

Related

presentViewController in AppDelegate with delay in iOS8

So I had a full working solution in iOS7 that displays a LoginViewController via presentViewController in the AppDelegate's didFinishLaunching.
Basically I am doing something like this:
UIViewController *backgroundViewController = ...
self.window.rootViewController = backgroundViewController;
[self.window makeKeyAndVisible];
[self.window.rootViewController presentViewController:loginViewController
animated:NO ...]
In iOS8 I see a jump. First I see the backgroundViewController then after about 1 second or so the login appears.
So, how can I prevent this jump in iOS8?
I am seeing that are a ton of developers with this kind of problem but still didn't find a solution.
Also a hack (for now), but just one line of code
Add the view of the view controller you're presenting to the window before presentation
UIViewController *viewController = [[UIViewController alloc] init];
[viewController.view setBackgroundColor:[UIColor greenColor]];
// Temporary iOS8 fix for 'presentation lag' on launch
[self.window addSubview:viewController.view];
[self.window.rootViewController presentViewController:viewController animated:NO completion:nil];
If you are presenting a navigation controller than add the navigation controller's view instead of its top view controller.
I have a quick hacky fix:
//Make a screenshot of the ViewController first, or use a real image if you want
__block UIImageView *fakeImageView = [[UIImageView alloc] initWithImage:image];
fakeImageView.frame = vc.view.frame;
[self.view addSubview:fakeImageView];
[self presentViewController:vc animated:animated completion:^{
[fakeImageView removeFromSuperview];
fakeImageView = nil;
}];
It is not good for long term, but can quickly fix this issue without changing too much code.
Waiting for better solutions.
You can set the window to an instance of a temporary controller.
self.window.backgroundColor = [UIColor whiteColor]; //do some styling etc.
self.window.rootViewController = [LoginViewController new];
[self.window makeKeyAndVisible];
From the set controller (LoginViewController) you can push your real login controller with the desired transition. Once the login sequence is over you can make a transition from the login controller to the default application root view controller.
[UIView transitionWithView:[AppGlobal sharedApp].applicationWindow
duration:0.75
options:UIViewAnimationOptionTransitionFlipFromLeft
animations:^{
[AppGlobal sharedApp].applicationWindow.rootViewController = [AppRootViewController new];
} completion:nil];
I have also faced the same problem in iOS8 and I found this solution:
ABCViewController *obj = [[ABCViewController alloc] initWithNibName:#"ABCViewController" bundle:nil];
CATransition *transition = [CATransition animation];
transition.duration = 0.4;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionMoveIn;
transition.subtype = kCATransitionFromBottom;
transition.delegate = self;
[self.navigationControler.view.layer addAnimation:transition forKey:nil];
[appDelegate.navigationControler obj animated:NO];
obj = nil;
I hope this solution can help you!
This should work:
call [loginViewController view]
Before presenting it.

NSTimer Stopwatch

I have a stopwatch that runs fine on my app however when I click on the back button to go to another view it stops. I want the stopwatch to keep running even when the user is viewing another page.
How do I accomplish this?
Ok added the code
code:
.h
UILabel *stopWatchLabel;
NSTimer *stopWatchTimer; // Store the timer that fires after a certain time
NSDate *startDate; // Stores the date of the click on the start button
#property (nonatomic, retain) IBOutlet UILabel *stopWatchLabel;
- (IBAction)onStartPressed:(id)sender;
- (IBAction)onStopPressed:(id)sender;
.m
- (void)viewDidUnload
{
[self setStopWatchLabel:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)updateTimer
{
NSDate *currentDate = [NSDate date];
NSTimeInterval timeInterval = [currentDate timeIntervalSinceDate:startDate];
NSDate *timerDate = [NSDate dateWithTimeIntervalSince1970:timeInterval];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"HH:mm:ss.SSS"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0.0]];
NSString *timeString=[dateFormatter stringFromDate:timerDate];
stopWatchLabel.text = timeString;
}
- (IBAction)onStartPressed:(id)sender {
startDate = [NSDate date];
// Create the stop watch timer that fires every 10 ms
stopWatchTimer = [NSTimer scheduledTimerWithTimeInterval:1.0/10.0
target:self
selector:#selector(updateTimer)
userInfo:nil
repeats:YES];
}
- (IBAction)onStopPressed:(id)sender {
[stopWatchTimer invalidate];
stopWatchTimer = nil;
[self updateTimer];
}
Well it stops because you're releasing the view that it lives in when you pop it off the stack.
have you tried making a stopwatch class that handles the timer independent of the view?
The stopwatch stops because you're releasing the view that controls it.
You need to make sure that the NSTimer is not linked to any view.
Perhaps you can use subviews or Modal View Controllers (I think that may work) in order to not have the stopwatch released when the view is.
You could also try sending the stopwatch information to the next view through variables, and then having the other view take over the stopwatch from there.
There are most definitely other methods as well.
Either way, you need to make sure that the NSTimer is not released.
Exactly, if you look at your "ViewDidUnload" you're setting it to nil when the view gets released which it does when you go to another view.
Something you can try is to have a class that's running the timer and sending it over to the view that way it's still running even though the view that displays it is not in memory.
MVC style!
Yeah, what everyone else says.
On the ViewDidUnload it is releasing it. Meaning it is basically ending the operation as soon as that ViewController is exited.
I Dont know if the problem is already solved or not but still.
Create the Object of NSTimer in App Delegate Class and then instantiate the timer of in .m file of app delegate in this way your NSTimer will stay in memory. and when you want to stop or pause the timer you can just invalidate that object from any view Controller.

iOS - Three buttons in Nav. Bar - not Tab Bar

I am building an app that sort of calls for three buttons in the top nav. bar. I don't want to do a tab bar, as there will be only one item in the tab.
In the code below, the left button (Edit) works fine. The right button - the link to cards also works fine. However the stupid Add button gives me an error that it cannot find the selector (insertNewObject).
If I take out my custom code and use the code that says "Works in a pinch", well.. it works.
I'd appreciate either knowing what I'm doing wrong, or else, another way of handling the problem at hand - linking to another part of the app.
TIA.
/*
// THIS WORKS IN A PINCH
// Set up the edit and add buttons.
self.navigationItem.leftBarButtonItem = self.editButtonItem;
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(insertNewObject)];
self.navigationItem.rightBarButtonItem = addButton;
[addButton release];
// END OF WHAT WORKS
*/
// create a toolbar to have two buttons in the right
UIToolbar* tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 105, 44.01)];
self.navigationItem.leftBarButtonItem = self.editButtonItem;
// create the array to hold the buttons, which then gets added to the toolbar
NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:3];
UIBarButtonItem* addButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(insertNewObject)];
addButton.style = UIBarButtonItemStyleBordered;
[buttons addObject:addButton];
[addButton release];
// create a standard "add" button
// create a flip button
UIBarButtonItem* flipButton = [[UIBarButtonItem alloc]
initWithTitle:#"Cards" style:UIBarButtonItemStyleBordered
target:self action:#selector(flipToFront)];
[buttons addObject:flipButton];
[flipButton release];
// stick the buttons in the toolbar
[tools setItems:buttons animated:NO];
[buttons release];
// and put the toolbar in the nav bar
UIBarButtonItem* rightButtonBar = [[UIBarButtonItem alloc] initWithCustomView:tools];
self.navigationItem.rightBarButtonItem = rightButtonBar;
[rightButtonBar release];
You may be missing a colon after the selector name. I think just putting in #selector(newObject) is a reference to a method with no parameters. #selector(newObject:) may work instead.

UIBarButtonType as a UIButton. Not on a NavigationBar!

I would like to use the better styled UIBarButtonType as a typical UIButton. This is just to use on a view not a navigationBar.
I don't really want to recreate the button in an image app and apply to the various states. I know how to do it the other way around so there must be a way.
Please help.
I tried this and it works :
UIButton *info = [UIButton buttonWithType:UIButtonTypeInfoLight];
UIBarButtonItem *infoBis = [[UIBarButtonItem alloc] initWithCustomView:info];
NSArray *array = [[NSArray alloc] initWithObjects:infoBis, nil];
[tool setItems:array animated:YES]; //tool = UIToolbar
don't forget to put a target on your UIBarButtonItem ;)
You cannot do that.
UIBarButtonItems are made just for UIToolbar and UINavigationBar.
You need to create your own background image. It isn't a big issue, just few lines.

how to make it so when a view loads, it does something?

I am making a quiz app, and I wanted it to have a saying at the end of the quiz. This saying is supposed to change based on an NSInteger (myScore). How would I code this? Any help would be helpful to me. Thanks!
You are going to create a UILabel based on an NSInteger:
NSInteger yourInteger;
UILabel *aLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 100);
[aLabel setText:#"Score %d", yourInteger];
[aLabel setBackgroundcolor:[UIColor clearColor]];
[aLabel setOpaque:NO];
[[self view] addSubview:aLabel];
actually %d is replaced with your integer

Resources