I am trying to set up a countdown timer. I have two view controllers: ViewController and EditViewController. ViewController has two labels hours and minutes. EditViewController has a UIDatePicker and a save button. When the user selects a time on the UIDatePicker and hits save, they are redirected back to the ViewController and the hour and minute labels are populated with the difference between the selected time and current time. This is automatically updated until the difference is 0. I have having issues trying to get the hour and minutes to display correctly on the ViewController labels. Here is my code:
ViewController.m
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.hourLabel.text = self.hourText;
self.minuteLabel.text = self.minuteText;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
EditViewController.h
#interface EditViewController : UIViewController
#property (weak, nonatomic) IBOutlet UIDatePicker *datePicker;
#property (weak, nonatomic) IBOutlet NSString *currentTimeString;
#property (weak, nonatomic) IBOutlet NSString *endTimeString;
- (IBAction)saveButton:(id)sender;
- (IBAction)cancelButton:(id)sender;
-(void)updateTime;
#end
EditViewController.m
#import "EditViewController.h"
#import "ViewController.h"
#interface EditViewController ()
#end
#implementation EditViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
{
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)saveButton:(id)sender
{
NSCalendar *now = [NSCalendar autoupdatingCurrentCalendar];
NSInteger currentTime = (NSHourCalendarUnit | NSSecondCalendarUnit);
self.datePicker.date = [now dateFromComponents:[now components:currentTime fromDate:self.datePicker.date]];
NSTimer *timer;
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(updateTime) userInfo:nil repeats:YES];
[self dismissViewControllerAnimated:YES completion:nil];
}
-(void)updateTime
{
NSInteger timeLeft = ((NSInteger)[self.datePicker.date timeIntervalSinceNow]);
NSInteger minutes = (timeLeft / 60) % 60;
NSInteger hours = (timeLeft / 3600) % 24;
ViewController *controller;
controller.hourText = [NSString stringWithFormat:#"%ldi",(long)hours];
controller.minuteText = [NSString stringWithFormat:#"%ldi",(long)minutes];
NSLog(#"The time is %ld",(long)timeLeft);
}
- (IBAction)cancelButton:(id)sender
{
[self dismissViewControllerAnimated:YES completion:nil];
}
#end
Any help is appreciated. Thanks in advance.
Have a look at here for the edited method from your code. This will full-fill your requirement.
#import "EditViewController.h"
#import "ViewController.h"
#import "AppDelegate.h"
#interface EditViewController ()
{
AppDelegate *appDelegate;
}
#end
#implementation EditViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
{
// Custom initialization
appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
}
return self;
}
-(void)updateTime
{
NSInteger timeLeft = ((NSInteger)[self.datePicker.date timeIntervalSinceNow]);
NSInteger minutes = (timeLeft / 60) % 60;
NSInteger hours = (timeLeft / 3600) % 24;
NSString *hourText = [NSString stringWithFormat:#"%ldi",(long)hours];
NSString *minuteText = [NSString stringWithFormat:#"%ldi",(long)minutes];
[appDelegate updateTime:hourText andMinute:minuteText];
}
and finally, define a method in AppDelegate class
- (void) updateTime:(NSString *)hourStr andMinute:(NSString *)minuteStr
{
for(id obj in [self.navigationController viewControllers])
{
if([obj isKindOfClass:ViewController])
{
ViewController *vcObj = (ViewController *)obj;
vcObj.hourLabel.text = hourStr;
vcObj.minuteLabel.text = minuteStr;
}
}
}
Several issues:
ViewController.m
self.hourLabel.text = self.hourText;
self.minuteLabel.text = self.minuteText;
Should be in viewWillAppear instead of viewDidLoad. What's happening is you set the text when the app loads. viewDidLoad will only fire once for that instance of the app ever. What you're needing to do is update it when you come back from the EditViewController, right? So if you put it in viewWillAppear, it will also get called when you come back and update it for you.
The next thing:
EditViewController.m
ViewController *controller;
controller.hourText = [NSString stringWithFormat:#"%ldi",(long)hours];
controller.minuteText = [NSString stringWithFormat:#"%ldi",(long)minutes];
NSLog(#"The time is %ld",(long)timeLeft);
This code snippet is declaring a new ViewController variable (well, most of it, you're missing the = [ViewController new] part). The problem with this is that you don't do anything with this controller. ViewController is actually already in your navigation stack (I'm assuming NavigationController since you said when the user goes back), so you need to use the reference to ViewController your app has already instantiated, not create a new one since you never use this guy anywhere.
Related
I want to change label.text but when I do it and check it with a NSLog it returns me a (null).
Here's my code:
NextViewController.h
#interface NextViewController (){
NSTimer *timerTen;
NSInteger countDown;
NSString *timer;
}
#end
#implementation NextViewController
static UILabel *lblTest;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)startCountDown{
countDown = 10;
timerTen = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(dismissView) userInfo:nil repeats:YES];
}
-(void)dismissView{
timer = [NSString stringWithFormat:#"%ld", (long)countDown];
_lbl_countDown.text = timer;
NSLog(#"\nCountDown: %#\n", self.lbl_countDown.text);
countDown--;
if (countDown <= 0) {
SectionViewController *sectionView = [[SectionViewController alloc] init];
[sectionView.presentedViewController dismissViewControllerAnimated:YES completion:nil];
}
}
Then I have a XIB file with a label and more things, so I've used a prop right there in the .h to change it from other classes, but I can't change it.
NextViewController.h
#import <UIKit/UIKit.h>
#interface NextViewController : UIViewController
#property (strong, nonatomic) IBOutlet UILabel *lbl_section;
#property (strong, nonatomic) IBOutlet UILabel *lbl_nextSection;
#property (strong, nonatomic) IBOutlet UILabel *lbl_countDown;
-(void)startCountDown;
#end
Hope anyone can help me cause I don't know what is happening here.
Thanks.
Call your method in ViewDidLoad or ViewWillAppear
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
[self startCountDown]
}
OR
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self startCountDown]
}
Try to call the function first using [self startCountDown] in the ViewDidLoad method
if the problem persist:
Try to use this : [_lbl_count setText#"myText"];
In the Identity Inspector, click on the label uncheck the 'Static Text' and check 'Enabled' and 'User Interaction Enabled'
Finally check that your variable timer is not null after your initialization.
timer = [NSString stringWithFormat:#"%ld", (long)countDown];
Hope that will be helpfull
I wrote this code to use a button tap to start a countdown from 10 to a segue:
In my h:
#property (strong, nonatomic) IBOutlet UILabel *countdownCounter;
#property (strong, nonatomic) IBOutlet UIButton *countdownStarter;
In my m:
static NSTimer *timer;
static int remainingCounts;
.
.
-(void)countDown
{
if (--remainingCounts == 0)
{
[timer invalidate];
timer = nil;
[self performSegueWithIdentifier:#"FromStartToBalancer" sender:self];
}
else
{
NSString * holdText = [NSString stringWithFormat:#"%d", remainingCounts];
[self.countdownCounter setText:holdText];
}
}
- (IBAction)goToBalancerPressed:(UIButton *)sender
{
self.countdownStarter.userInteractionEnabled = NO;
self.countdownStarter.alpha = .3;
remainingCounts = 10;
timer = [NSTimer scheduledTimerWithTimeInterval:1
target:self
selector:#selector(countDown)
userInfo:nil
repeats:YES];
}
I have an app, where I making some calculation, and need to transfer result of this calculation to ViewController3 and show the result there. Now I use label in same ViewController2 where I have calculation. Thank you for your help.
ViewController2.h
#import <UIKit/UIKit.h>
#interface ViewController2 : UIViewController<UITextFieldDelegate>
#property (weak, nonatomic) IBOutlet UILabel *gasPrice;
#property (weak, nonatomic) IBOutlet UILabel *gasCarMileage;
#property (weak, nonatomic) IBOutlet UITextField *perGalon;
#property (weak, nonatomic) IBOutlet UITextField *miles;
#property(nonatomic, copy, readonly) NSString *result;
- (IBAction)getIt:(id)sender;
#end
ViewController2.m
#import "ViewController2.h"
#import "ViewController3.h"
#interface ViewController2 ()
#end
#implementation ViewController2
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
//Start calculation
- (IBAction)getIt:(id)sender; {
float perGalon = ([_perGalon.text floatValue]);
float miles = ([_miles.text floatValue]);
float mileCost = perGalon / miles;
[self performSegueWithIdentifier:#"viewController3" sender: nil];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"viewController3"]) {
ViewController3 *viewController3 = [segue destinationViewController];
viewController3.result = [[NSString alloc] initWithFormat: #"Every mile you drive
will cost you $ %f", mileCost];
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.perGalon.delegate = self;
self.miles.delegate = self;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
return [textField resignFirstResponder];
}
#end
ViewController3.h
#import <UIKit/UIKit.h>
#interface ViewController3 : UIViewController
#property(nonatomic, copy) NSString *result;
#end
ViewController3.m
#import "ViewController3.h"
#import "ViewController2.h"
#interface ViewController3 ()
#end
#interface ViewController2 ()
#property(nonatomic, copy, readwrite) NSString *result;
#end
#implementation ViewController3
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
You can define a string property in View controller 3 which you will set when you push view controller 3 from view controller 2.
ViewController3 interface declaration
#interface ViewController2 ()
#property(nonatomic, copy) NSString *result;
#end
In ViewController 2, you will implement this line at the last of your getIt() method.
[self performSegueWithIdentifier:#"viewController3" sender: nil]
And implement another method called prepareForSegue as follows
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"viewController3"]) {
ViewController3 *viewController3 = [segue destinationViewController];
viewController3.result = "YOUR CALCULATION RESULT HERE"
}
}
Bear in mind, you have to set identifier for view controller 3, go to storyboard and select the view for view controller 3 and On identify inspector, specified Storyboard ID as "viewController3".
Use a protocol,
In ViewController2.h
#protocol ViewController2Delegate;
#property (strong, nonatomic) id<ViewController2Delegate> delegate;
#protocol BexAPIClientDelegate <NSObject>
- (void)ViewController2:(ViewController2 *)vc didCalculateCost:(CGFloat)cost;
#end
Then at the end of the getIt() call the delegate
[self.delegate viewController2:self didCalculateCost:gasCostPerMile];
You need to set ViewController3 to be ViewController2's delegate when you load it. Also you will need to setup ViewController3 to conform to the protocol.
Using the protocol is worth the effort as it will make your code clear and easy to manage.
Retain a string in VC3. While initializing the VC3 object, set the value from VC2. Update the label in VC3(in viewDidLoad method)
I have an array, players, with two strings inside it: player1 and player2. Here is my .h file:
#import <UIKit/UIKit.h>
#interface hardOne : UIViewController {
UISwitch *hard1ON;
NSMutableArray *players;
NSString *player1, *player2;
}
#property (nonatomic, retain) IBOutlet UISwitch *hard1ON;
#property (nonatomic) BOOL switchState;
#property (nonatomic, retain) NSMutableArray *players;
- (IBAction) switchValueChanged;
#end
The array is initialized in the viewDidLoad then the data is entered into that array in two IBActions in my .m file:
#import "hardOne.h"
#interface hardOne () <UITextFieldDelegate>
#property (nonatomic, strong) IBOutlet UITextField *textFieldOne;
#property (nonatomic, strong) IBOutlet UITextField *textFieldTwo;
#end
#implementation hardOne
#synthesize hard1ON;
#synthesize players;
#synthesize textFieldOne;
#synthesize textFieldTwo;
BOOL switchState;
int counter = 0;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[hard1ON setOn:switchState animated:NO];
//read player names to user defaults
[textFieldOne setText:[[NSUserDefaults standardUserDefaults] stringForKey:#"player1"]];
[textFieldTwo setText:[[NSUserDefaults standardUserDefaults] stringForKey:#"player2"]];
self.players = [[NSMutableArray alloc] init];
NSLog(#"%#",self.players);
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction) switchValueChanged
{
counter += 1;
if (counter % 2 == 0) {
switchState = 0;
} else {
switchState = 1;
}
if (hard1ON.on) {
[[NSNotificationCenter defaultCenter] postNotificationName:#"theChange" object:nil];
} else {
[[NSNotificationCenter defaultCenter] postNotificationName:#"theChange2" object:nil];
}
}
- (IBAction) returnKey1
{
player1 = [textFieldOne text];
[self.players addObject:(player1)];
//set player1's name to user defaults
[[NSUserDefaults standardUserDefaults] setValue:[textFieldOne text] forKey:#"player1"];
}
- (IBAction) returnKey2
{
player2 = [textFieldTwo text];
[self.players addObject:(player2)];
//set player2's name to user defaults
[[NSUserDefaults standardUserDefaults] setValue:[textFieldTwo text] forKey:#"player2"];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return NO;
}
#end
If I use NSLog in the second IBAction, once it is complete, the array is correctly displayed in the console with the strings player1 and player2, however if I try to use the array anywhere else it is null. Could anyone point me in the right direction?
You've got two definitions for players.
One is a property. It's never initialized and so it's null. You use it as self.players and backed by the instance variable _players.
One is an instance variable. It's initialized in viewDidLoad. It's not nil.
This is almost surely a mistake.
I would try adding the array as a private instance variable i.e add it to the .m file in the #interface with
NSMutableArray *players;
then you should be able to access the array just by using "players" instead of self.players. This should be then be available throughought the whole of your class. If this doesn't work then I would say the problem doesn't lie within the scope of your variable but rather the with some other code.
pass data from FirstViewController to DetailViewController. i can not set the text of label in DetailViewController; FirstViewController is a tableview and it is good.
i use method updateRowNumber to set the rowNumber . and in DetailViewController, i can use debugger to see the rowNumber is correct. but the label's text is not showed on the view.
anyone can help me out?
in my FirstViewController
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (dvController == nil)
{
DetailViewController *aController = [[DetailViewController alloc] initWithNibName:#"DetailViewController" bundle:nil];
self.dvController = aController;
[aController release];
}
[[self navigationController] pushViewController:dvController animated:YES];
[dvController updateRowNumber:indexPath.row];
}
in my DetailViewController.h
#import <UIKit/UIKit.h>
#interface DetailViewController : UIViewController
{
int rowNumber;
IBOutlet UILabel *message;
}
#property(readwrite) int rowNumber;
#property(nonatomic, retain) IBOutlet UILabel *message;
- (void) updateRowNumber:(int) theindex;
#end
in my DetailViewController.m
#import "DetailViewController.h"
#interface DetailViewController ()
#end
#implementation DetailViewController
#synthesize message, rowNumber;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void) updateRowNumber: (int) theindex
{
rowNumber = theindex + 1;
message.text = [NSString stringWithFormat:#"row %i was clicked", rowNumber];
}
- (void)dealloc
{
[message release];
[super dealloc];
}
- (void)viewDidLoad
{
message.text = [NSString stringWithFormat:#"row %i was clicked ", rowNumber];
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Do any additional setup after loading the view from its nib.
}
- (void)viewWillAppear:(BOOL)animated
{
//message.text = [NSString stringWithFormat:#"row %i was clicked ", rowNumber];
[super viewWillAppear: animated];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear: animated];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
You'll need to learn how the view is loaded, the process is well described in the documentation
What happens is that the view and all the outlets are nil until the view is loaded, you can make sure it is loaded by calling self.view; before configuring the outlet at updateRowNumber:
Please also note, you are better to call [super viewDidLoad] in the beginning of the overridden viewDidLoad, it makes sense as you need to let UIViewContorller to do it's staff before you do some customized logic, the dealloc is different as you need to do your logic before the standard NSObject -dealloc fires. Hope it's understandable.
my application has 3 views
in view1 only one button (play level 1) that pushes to veiw2 and updates titlelabel in view2
when view2 loads and appears a timer starts to count down and update another label in view2 itself, also view2 has button that pauses the timer and pushes to view3
in view3 only one button that pop back to view2 and resumes thee timer
my problem is that when i pop from view3 to view2 the timer is still paused
any help please?
Thanks & kind regards.
view1.h
#import <UIKit/UIKit.h>
#interface view1 : UIViewController
-(IBAction)nextpage;
#end
view1.m
#import "view1.h"
#import "view2.h"
#interface view1 ()
#end
#implementation view1
-(IBAction)nextpage{
view2 *obj = [[view2 alloc] init];
[self.navigationController pushViewController:obj animated:YES];
[obj.leveltitle setText:#"Level"];
obj.time=10;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[self.navigationController setNavigationBarHidden:YES];
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#end
view2.h
#import <UIKit/UIKit.h>
#interface view2 : UIViewController
{
int time;
NSTimer *timer;
BOOL paused;
}
#property (nonatomic, retain) NSTimer *timer;
#property (readwrite, nonatomic) int time;
#property (nonatomic) BOOL paused;
#property (readwrite, retain) IBOutlet UILabel *leveltitle;
#property (nonatomic, readwrite) IBOutlet UILabel *label;
-(IBAction)back;
-(IBAction)pause;
#end
view2.m
#import "view2.h"
#import "view3.h"
#interface view2 ()
#end
#implementation view2
#synthesize leveltitle, label;
#synthesize time, timer;
#synthesize paused;
-(void)countDown2
{
if (paused == NO && time>0)
{
time = time -1 ;
view3 *obj = [[view3 alloc] init];
obj.sss = time;
if (time == 0)
{
[timer invalidate];
}
[label setText:[NSString stringWithFormat:#"%i",time]];
}
}
-(IBAction)back{
[self.navigationController popViewControllerAnimated:YES];
}
-(IBAction)pause{
view3 *obj = [[view3 alloc] init];
[self.navigationController pushViewController:obj animated:YES];
paused = YES;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
paused = NO;
timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(countDown2) userInfo:nil repeats:YES];
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#end
view3.h
#import <UIKit/UIKit.h>
#interface view3 : UIViewController
{
int sss;
}
#property (nonatomic) int sss;
-(IBAction)back;
#end
view3.m
#import "view3.h"
#import "view2.h"
#interface view3 ()
#end
#implementation view3
#synthesize sss;
-(IBAction)back{
view2 *obj = [[view2 alloc] init];
obj.paused=NO;
obj.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(cont) userInfo:nil repeats:YES];
//[obj.timer fire];
[self.navigationController popViewControllerAnimated:YES];
}
-(void)cont{
view2 *obj = [[view2 alloc] init];
obj.time = sss;
if (obj.paused == NO && time>0)
{
obj.time = obj.time -1 ;
if (obj.time == 0)
{
[obj.timer invalidate];
}
[obj.label setText:[NSString stringWithFormat:#"%i",obj.time]];
}
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#end
It is because in view3's back method, you created a new instant of view2 name obj and set paused=NO. But that obj was not the same instant of view2 when you pop back. What you need to do is to use a delegate method pattern, so that view3 will have a callback method in view2 to set the paused to NO.
For an example of delegate method pattern, check out an example from this SO.
Edit: sample callback code:
Make these addition/changes to your code:
In view2.h:
#import "view3.h"
#interface view2 : UIViewController <view3Delegate>
In view2.m:
-(IBAction)pause{
view3 *obj = [[view3 alloc] init];
obj.delegate = self; //adding this line
[self.navigationController pushViewController:obj animated:YES];
paused = YES;
}
-(void)setPausedValue:(BOOL)value //this is the new method callback from view3 to set paused value
{
paused = value;
}
In view3.h:
#import <UIKit/UIKit.h>
#protocol view3Delegate <NSObject>
-(void) setPausedValue:(BOOL)value;
#end
#interface view3 : UIViewController
{
int sss;
}
#property (assign) id <view3Delegate> delegate;
#property (nonatomic) int sss;
-(IBAction)back;
#end
In view3.m:
#implementation view3
#synthesize delegate;
-(IBAction)back{
// view2 *obj = [[view2 alloc] init];
// obj.paused=NO;
[self.delegate setPausedValue:NO]; //make callback and set value
obj.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(cont) userInfo:nil repeats:YES];
//[obj.timer fire];
[self.navigationController popViewControllerAnimated:YES];
}