i am trying to using a Nstimer view transition on multiple view controllers but if i copy and paste it i get an error saying Duplicate declaration of method . how would i fix it ? please explain in your answer how to do this . i found out how to do this from this link .
http://x-code-tutorials.com/2013/06/19/nstimer-trigger-uiviewcontroller-transition/
- (void)viewDidAppear:(BOOL)animated {
[self startTimerMethod];
}
- (void) startTimerMethod {
transitionTimer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:#selector(transitionView) userInfo:nil repeats:NO];
}
- (void) transitionView {
[self performSegueWithIdentifier:#"viewTransition" sender:self];
}
- (void)viewDidAppear:(BOOL)animated {
[self startTimerMethod];
}
- (void) startTimerMethod {
transitionTimer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:#selector(transitionView) userInfo:nil repeats:NO];
}
- (void) transitionView {
[self performSegueWithIdentifier:#"viewTransition" sender:self];
}
You have copy and paste below three methods two times let it define or declare single time:-
-(void)viewDidAppear:(BOOL)animated
-(void) startTimerMethod
-(void) transitionView
Related
I create a subclass of UILabel called NewLabel, in NewLabel.m, codes like this
+ (NewLabel*)addLabelIntoView:(UIView*)view
{
NewLabel *label = [[NewLabel alloc] init];
//some codes
[view addSubview:label];
[NSTimer scheduledTimerWithTimeInterval:1.0f target:view selector:#selector(callActionInVC) userInfo:nil repeats:NO];
return label;
}
and there's a UIViewController, codes in UIViewController.m
- (void)viewDidLoad {
[NewLabel addLabelIntoView:self.view]
}
- (void)callActionInVC {
//do some actions
}
I want to call the function callActionInVC of UIViewController.m in NewLabel.m, and in NewLabel.m, target:view is not right, What should I change to? What's the target of a UIViewController in a subclass?
Or is there any ideas to make this demand?
the target is wrong
[NSTimer scheduledTimerWithTimeInterval:1.0f target:view selector:#selector(callActionInVC) userInfo:nil repeats:NO];
You should set target as the controller.
[NSTimer scheduledTimerWithTimeInterval:1.0f target:<your controller> selector:#selector(callActionInVC) userInfo:nil repeats:NO];
You may need to create a category to get the controller.
- (UIViewController *)fy_parentController{
UIResponder *responder = [self nextResponder];
while (responder) {
if ([responder isKindOfClass:[UIViewController class]]) {
return (UIViewController *)responder;
}
responder = [responder nextResponder];
}
return nil;
}
The final code looks like this:
[NSTimer scheduledTimerWithTimeInterval:1.0f target:view.fy_parentController selector:#selector(callActionInVC) userInfo:nil repeats:NO];
So basically I have two methods that I run when the user clicks an Annotation in the map.
- (void) methodA{
[UIView animateWithDuration:kRequestButtonAnimationDuration animations:^(void){
self.button.transform = CGAffineTransformMakeTranslation(0,-(self.navigationController.view.frame.size.height - self.button.frame.origin.y)); }]; }
- (void) methodB:(double)value andBoolean:(BOOL) boolean{
if (self.timer) {
[self.timer invalidate];
self.timer = nil;
}
if (boolean) {
self.timer = [NSTimer scheduledTimerWithTimeInterval:value target:self selector:#selector(pullButtonUp) userInfo:nil repeats:NO];
} else{
self.timer = [NSTimer scheduledTimerWithTimeInterval:value target:self selector:#selector(showTabBar) userInfo:nil repeats:NO];
}}
-(void)pullButtonUp{
[(LLTabBarViewController*)self.tabBarController showTabBarAnimated];}
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view{
[self methodA];
[self methodB:2.0 andBoolean:TRUE];
}
So during the first time I select an annotation, it works well: it runs methodA, then methodB after 2.0 seconds.
But when I try it again, it runs both A and B at the same time (basically methodA waits 2.0 seconds to execute).
Could this be related to Loops?
How should I track this problem? Any instrument? Which one?
I am a bit lost, help me please!
I think this is what you want to do
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
__weak typeof(self) weakSelf = self;
BOOL arbitraryBool = YES;
[UIView animateWithDuration:2
animations:^{
self.button.transform = CGAffineTransformMakeTranslation(0,-(self.navigationController.view.frame.size.height - self.button.frame.origin.y));
} completion:^(BOOL finished) {
if (finished) {
if (arbitraryBool) {
[weakSelf pullButtonUp];
} else {
[weakSelf showTabBar];
}
}
}]:
}
in xcode, when i log my NSTimer, It starts atviewDidAppear and stops on viewdiddisappear which is fine, but when I go to an exit segue, the timer resumes (which I don't want to happen because its getting doubled).how do I properly stop it so it won't get doubled when called again?
..when I put in on viewdidload.sure it doesn't get doubled, but it won't start again since I invalidate it viewDidDisappear here is my code
-(void)viewDidAppear:(BOOL)animated{
if (self.timeInc.isValid == NO){
self.timeInc = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:#selector(updateDetailCounter:) userInfo:([Dateformatter2() dateFromString:_DetailModal[1]]) repeats:YES];
}
}
-(void)viewDidDisappear:(BOOL)animated{
[self.timeInc invalidate];
}
You can try this, in the past I had the same problem in viewDidAppear, viewDidDisappear
- (void) viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
if (self.timerInc) {
[self.timerInc invalidate];
self.timerInc = nil;
}
_timerInc = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(doSth) userInfo:nil repeats:YES];
}
- (void) viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
[self.timerInc invalidate];
self.timerInc = nil;
}
I want to create a button that takes me to another view controller and automatically starts a countdown from 3 to 0, but i don't know how to set the countdown on the other view controller. Here's the code i tried:
#implementation TestViewController
-(IBAction)test:(id)sender {
CountdownViewController *cdvc = [[CountdownViewController alloc]
initWithNibName:#"CountViewController" bundle:nil];
[self.navigationController pushViewController:cdvc animated:YES];
}
#implementation CountdownViewController
int maintInt = 3;
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(countDown) userInfo:nil repeats:YES];
-(void)countDown {
maintInt -= 1;
count.text = [NSString stringWithFormat:#"%i", maintInt];
if(maintInt==1){
[timer invalidate];
}
}
You have to place your countdown code in a method, which you should call in viewDidLoad or viewWillAppear method regarding your needs.
#implementation TestViewController
-(IBAction)test:(id)sender {
CountdownViewController *cdvc = [[CountdownViewController alloc]
initWithNibName:#"CountViewController" bundle:nil];
[self.navigationController pushViewController:cdvc animated:YES];
}
#end
#implementation CountdownViewController
- (void)viewDidLoad {
[super viewDidLoad];
// .. customize your views here ..
// Initialize the timer once your view has been loaded.
//
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(countDown:) userInfo:nil repeats:YES];
}
static int maintInt = 3;
- (void)countDown:(NSTimer *)timer {
count.text = [NSString stringWithFormat:#"%i", maintInt];
maintInt -= 1;
if (maintInt == 0 && timer) {
[timer invalidate];
timer = nil;
}
}
#end
If I use:
[NSTimer scheduledTimerWithTimeInterval:.1 target:self selector:#selector(myMethod) userInfo:nil repeats:YES];
How can I invalidate this, as it's a class method I don't have access to a pointer to it?
You can make something like this:
[NSTimer scheduledTimerWithTimeInterval:.1 target:self selector:#selector(myMethod:) userInfo:nil repeats:YES];
(note ":" after myMethod)
- (void) myMethod: (id) sender
{
if ([sender isKindOfClass:[NSTimer class]])
{
NSTimer* timer = (NSTimer*) sender;
[timer invalidate];
}
}
It's a class method that returns a timer. You invalidate that timer.