Do we always needs to dismissModelViewController that we had presented?
-
18-01-2021 - |
Question
I have Edited whole Question
What is the difference between presenting the ModelView Controller and dismissing the modal view controller in following situation??
- HomeView
- TableItemSelection View
- PickerView
In HomeView
I am writing code in viewWillAppear
to fetch values from NSUserDefault that is set from TableView. Below is my code for viewDidLoad (for initial values) and viewWillAppear(when new value from tableview)
- (void)viewDidLoad
{
[super viewDidLoad];
actionMinutes = @"0";
actionSeconds = @"0";
restMinutes = @"0";
restSeconds = @"0";
}
- (void) viewWillAppear:(BOOL)animated
{
actionMin = [actionMinutes intValue];
actionSec = [actionSeconds intValue];
restMin = [restMinutes intValue];
restSec = [restSeconds intValue];
NSLog(@"acMin:%d acSec:%d reMin:%d reSec:%d",actionMin,actionSec,restMin,restSec);
}
In TableItemSelection View
I am presenting this view from HomeView. Now i want to set value of NSString in HomeView based on Table's didSelectRowAtIndex Method. I am using NSUserDefault
to set the value.
And with Done Button touch i am presenting HomeView. (Actually I have to dismissModalViewController) But when i use dismiss I am not able to get values in NSString of HomeView. I am getting values of the table from PickerView. (I am instructed to do that). Below is my code for Table view on DONE button touch
HomeView *homeView = [[HomeView alloc] init];
[homeView.view setBackgroundColor:[UIColor clearColor]];
[homeView.view setFrame:CGRectMake(0 ,40, 320, 460)];
homeView.actionMinutes = [[NSUserDefaults standardUserDefaults] valueForKey:@"actionMinute"];
homeView.actionSeconds = [[NSUserDefaults standardUserDefaults] valueForKey:@"actionSecond"];
homeView.restMinutes = [[NSUserDefaults standardUserDefaults] valueForKey:@"restMinute"];
homeView.restSeconds = [[NSUserDefaults standardUserDefaults] valueForKey:@"restSecond"];
homeView.song = [[NSUserDefaults standardUserDefaults] valueForKey:@"songstring"];
NSLog(@"%@",homeView.actionMinutes);
[self presentModalViewController:homeView animated:YES];
//[self dismissModalViewControllerAnimated:YES]; // if this method is used then no values are passed to HomeView
[homeView release];
In PickerView I am fetching the values from pickerview and then store it in UserDefault. below is my code for pickerview
NSUserDefaults *actionTime = [NSUserDefaults standardUserDefaults];
[actionTime setValue:min forKey:@"actionMinute"];
[actionTime setValue:sec forKey:@"actionSecond"];
So why exactly i am not able to get UserDefault values when dismissing ModelView.??? Does presenting a new view everytime will make a stack of views???
Solution
@DShah: Yes most definitely..!!
You need to dismiss every modal view.
Also Please post the code you are using.
Also keep in mind that you need to put the NSUserDefaults line first (the line which you use assign NSString value to NSUserDefaults) and then put the line where in you dismissModalViewControllerAnimated:
.
If you require more help please leave me a comment.
Hope this helps you.
OTHER TIPS
I think you are trying to store integer value in NSUserDefaults
then you have to
Use setInteger:forKey: instead of setObject:forKey:
. An integer is not an object, it's a primitive type.
To retrieve it use integerForKey:.
This is the final answer through which i am able to solve my problem
Here is a code on Done button touch event...
- (IBAction) btnDonePressed:(id)sender {
aurioTouchAppDelegate *appDelegate = (aurioTouchAppDelegate *) [[UIApplication sharedApplication] delegate];
appDelegate.homeView.actionMinutes = [[NSUserDefaults standardUserDefaults] valueForKey:@"actionMinute"];
appDelegate.homeView.actionSeconds = [[NSUserDefaults standardUserDefaults] valueForKey:@"actionSecond"];
appDelegate.homeView.restMinutes = [[NSUserDefaults standardUserDefaults] valueForKey:@"restMinute"];
appDelegate.homeView.restSeconds = [[NSUserDefaults standardUserDefaults] valueForKey:@"restSecond"];
// [self presentModalViewController:homeView animated:YES];
[self dismissModalViewControllerAnimated:YES]; // if this method is used then no values are passed to HomeView
}