Segue data transfer - uistoryboardsegue

I tried once, but I think I did not explain myself clearly enough. Im a newbie so forgive me my lack of lingo.
I am trying to pass data from a UItextlabel's text through a modal segue to a second view controller that calculates its contents into two answers. Oh should mention it is storyboard based one!
It looks something like this:
(FirstView Controller)
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
NSLog(#"prepareForSegue: %#", segue.identifier);
if
([segue.destinationViewController isKindOfClass:[SampleDelayCalculator2ViewController class]])
{
D = d1.text;
TT = T1.text;
FQ = Fq1.text;
NSLog(#"Transfering Data");
//sending information to new view controller with a segue
SampleDelayCalculator2ViewController *viewcontrollerB = (SampleDelayCalculator2ViewController*)segue.destinationViewController;
viewcontrollerB.d.text=self.D;
viewcontrollerB.T.text=self.TT;
viewcontrollerB.Fq.text=self.FQ;
}
return;
}
This reads straight from the UITextLabels and is performed every time button Calculate is triggered.
Then it goes to a second View
- (void)viewDidLoad
{
[self calculate];
[super viewDidLoad];
}
-(void) calculate{
//asigning the values from prepare for segue method to floats
NSLog(#"Value of string is %#", T.text);
Tf = ([T.text floatValue]);
Fqf = ([Fq.text floatValue]);
df = ([d.text floatValue]);
NSLog(#"Value of string is %f", Tf);
...
And then it continues to something else.
My main problem is that whenever T.text or Tf is printed it shows nill or 0.00000 which i obviously did not put in the UITextLabel. How can I solve this problem?
I tried to implement the calculate in ViewDidAppear but that did not change anything.
I hope I made myself clear :D

Try something like this:
Be sure to give your segue a name in the storyboard.
In your first viewcontroller:
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier]isEqualToString:#"segueToHomeDetails"]) {
SampleDelayCalculator2ViewController *viewcontrollerB = (SampleDelayCalculator2ViewController*)segue.destinationViewController;
viewcontrollerB.d = d1.text; // don't use viewcontrollerB.d.text it won't set the text in the next controller
viewcontrollerB.T = T1.text;
viewcontrollerB.Fq = Fq1.text;
}
}
In your 2nd viewcontroller:
Use the properties that you set up in your 2nd viewcontroller.h file.

Related

how to populate an array or any object from one view controller to another when we present the view using story board

I have implemented my iphone app using story board
In my app *i need to present a view from one view to another view not the pus*h,
I have created a screen in story board xib for the second view .
i set up the connection for button in the first view to the second view with 'present' not pust.
Now i need to populate an array to the second view which was presented
i tried following ways but not work out
1:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(UIButton *)sender
{
//Populate detail to BuyView mail page
if ([segue.id
entifier isEqualToString:#"BuyView"])
{
BuyViewController *destViewController = segue.destinationViewController;
destViewController.itemDetailsArray = [_itemDetailsArray objectAtIndex:sender.tag] ;
}
}
2:
- (IBAction)buyItemClick:(UIButton *)sender
{
BuyViewController *destViewController = [[BuyViewController alloc] init];
destViewController.itemDetailsArray = [_itemDetailsArray objectAtIndex:sender.tag] ;
}
On top of what #rdelmar has stated you are passing an object of which may or may not be an array.
Which in turn would mean
destViewController.itemDetailsArray
Would be pointing to an object which may not be an array.
I think what you want to do is
destViewController.itemDetailsArray = _itemDetailsArray;

ViewController and SegmentedControl

I have ViewCtrl1, and StartBtn:
-(IBAction)startbtn
{
ViewCtrl2 *vc2 = [[ViewCtrl2 alloc]init];
[self presentModalViewController:vc2 animated:YES];
}
I have also ViewCtrl2, UISegmentedControl, and ExitBtn:
- (IBAction)exitIt {
[self dismissModalViewControllerAnimated:YES];
}
If in ViewCtrl2 i select segment 2, or 3. When I press ExitBtn, will show ViewCtrl1, and if I press StartBtn from ViewCtrl1, my ViewCtrl2 shows default selected segment, not changes that i made recently.
How can i fix it?
You creating new vc2 each time and of course it shows default values. Initialise it once and reuse.

perpareForSegue - Passing image name - Blank at First

I have a thumbnail view controller with two thumbnails. Each have an IBAction setting the image name. The next view should display the full image.
The first time I select a button, the new view opens but is completely blank. I go back to the previous screen (navigation controller) and select the button again and the full image screen shows with the correct full image.
Back out, select the second button and the full image of the first button is displayed.
It appears that the full image is off by one iteration.
Below are some code snippets.
thumbnailViewController.M
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
fullImageViewController *targetVC = (fullImageViewController*)segue.destinationViewController;
targetVC.fullImageName = _imageName;
}
- (IBAction)running1 {
_imageName = #"img_running1.png";
}
- (IBAction)running2 {
_imageName = #"img_running2.png";
}
fullImageViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
_fullImage.image = [UIImage imageNamed: _fullImageName];
}
Any suggestions.
I'm guessing that you have both the IBAction and the Segue hooked up to your buttons? If so this is your issue. The segue occurs before the IBAction. The solution is instead of having the button hooked up to the segue have the IBAction perform it.
- (IBAction)running1 {
_imageName = #"img_running1.png";
[self performSegueWithIdentifier:#"yourSegue" sender:self];
}
This will correct the order by giving you control. otherwise I believe its segue then action.

updating value of modal view variable

I'm trying to make a modal view which displays the champion of my app.
there's a NSMutableString variable called champ in modal view,
which is supposed to be updated by returnChamp function in main view.
the champ string is correctly set in main view,
but in modal view, the champ value appears as (null).
In fact, it seems it doesn't even go into the returnChamp function.
so apparently something wrong with my calling or implementing returnChamp,
but I have another function that does the similar, and that works fine.
could anyone please help me?
-(void) mainView{
.....
champ = [[currentPlayers objectAtIndex:playerIndex] retain];
NSLog(#"%#",champ);
modalWinner = [[winner alloc] init];
modalWinner.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:modalWinner animated:YES];
}
- (NSMutableString *) returnChamp{
NSLog(#"returnChamp");
return champ;
}
//in modalWinner
-(void) modalView{
..............
champName = [[NSMutableString alloc] init];
NSLog(#"%#", [(MainViewController *)self.parentViewController returnChamp]);
champName = [(MainViewController *)self.parentViewController returnChamp];
UIImage *champImage = [UIImage imageNamed:champName];
}
self.parentViewController is probably not actually a reference to your object. For some reason, it seems that the framework always insists on setting a UINavigationController as self.parentViewController - even for modals, and to the extent that it will create one if there isn't already one. This is probably going unnoticed because you're casting it to your MainViewController type.
You'll need to find a different way of making your original object available to be communicated with, or perhaps pass the appropriate value to the newly-instantiated controller before you present it.
For example, if you add a champName property to the modal class, you can do:
modalWinner = [[ModalWinnerViewController alloc] init];
modalWinner.champName = myValue; /* Set value before presenting controller */
[self presentModalViewController:modalWinner animated:YES];
There will probably be some code needed to update the UI with this value. The viewWillAppear method of the modal view controller is a good place for this as it is called by the framework immediately before the view is presented.
Note that this property-based approach could be used to keep a reference to your intended parent object, as well. And see here for a different approach to solving a similar problem.

Is it possible to select a row in a **previous** UITableview

Is it possible to select a row in a previous UITableview.
I provided image samples to get a more clear picture of what is happening exactly.
http://img186.imageshack.us/img186/933/picture8a.png
http://img405.imageshack.us/img405/9327/picture9d.png
http://img200.imageshack.us/img200/5386/picture10thm.png
At the morning workout screen , one can select a row and behind it so it will play a Movie.
If the movie plays you can press the secondbutton and it will take you to the final table.
If you press the backbutton you will simply return to the previous screen.
Now here is where my problem lies.
If i'm in my final screen after pressing the secondbutton and I press on the backbutton it would be great if it could play the previous video( in other words the video connected to the previously selected cell)
So if it's possible for me to create a function or some sort of action that can actually select the previously selected row from the first field ( for instance Lunge Forward in this example ).
perhaps with some like
previousRow = [ self.tableView indexPathForSelectedRow];
[self tableView:[self tableView] didSelectRowAtIndexPath:previousRow];
just doing something there
Even if it's not possible I would appreciate it if someone would let me know.
edit:
Here is some code behind the cell when it's going to play a video
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//Get the dictionary of the selected data source.
NSDictionary *dictionary = [self.tableDataSource objectAtIndex:indexPath.row];
//check to see if video must be played
NSString *playVids = [dictionary objectForKey:#"playVids"];
finalscreen = [dictionary objectForKey:#"finalScreen"];
if(playVids == nil )
{
if(CurrentLevel != 0 )
{
if( finalscreen == nil )
{
NSString *movies = [dictionary objectForKey:#"movieID"];
NSURL *movie = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:movies ofType:#"m4v"]];
theMovie = [[MPMoviePlayerController alloc] initWithContentURL:movie];
[theMovie setOrientation:UIDeviceOrientationPortrait animated:NO];
[theMovie setScalingMode:MPMovieScalingModeAspectFit];
theMovie.backgroundColor = [ UIColor whiteColor];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(myMovieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:theMovie];
[theMovie play];
[self getToolBarStuff];
lblTitle.text = [dictionary objectForKey:#"Title"];
[[[UIApplication sharedApplication]keyWindow]addSubview:toolbar2];
[[[UIApplication sharedApplication]keyWindow]addSubview:imageView];
[[UIApplication sharedApplication]keyWindow]addSubview:lblTitle];
}
}
else
{
WebViewController *web = [[WebViewController alloc] initWithNibName:#"WebView" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:web animated:YES];
[web release];
}
}
else {
//Prepare to tableview.
rvController = [[RootViewController alloc] initWithNibName:#"RootViewController" bundle:[NSBundle mainBundle]];
//Increment the Current View
rvController.CurrentLevel += 1;
//Set the title;
rvController.CurrentTitle = [dictionary objectForKey:#"Title"];
//Push the new table view on the stack
[self.navigationController pushViewController:rvController animated:YES];
rvController.tableDataSource = Children;
[rvController release];
}
}
I get my info/feeds from a dictionaryfile and also different tags in my dictionary tell my tableview when to play the video and when not( playVids ).
so to be exact I have a start screen which brings you to a second screen and that one brings you to the morning workout screen and from there if you click a cell it will play a (different with every cell) video. I can't use the standard design from apple(with the backbutton above) since this assignment specifically asks to use their own design.
That's the reason why i created my own tabbar and backbutton.
My backbutton looks like this
-(void)back_clicked:(id)sender
{
[self.navigationController popViewControllerAnimated:YES];
CurrentLevel--;
switch( CurrentLevel ) {
case 0;
[toolbar removeFromSuperView];
default:
break;
}
}
So if I'm in the last screen I just pop that screen to return to my previous one( the morning workout). And what actually has to be done is pop that screen and play the last video. I thought I could do that by popping the last screen and then selecting the previously clicked cell in morning workout.
Any thoughts on that.
I am not sure I understand your problem exactly here. Can you show a bit more code? You have 3 view controllers, right? Are you using a standard navigation controller pattern with push/pop to get from one to the other? It looks like maybe you aren't since the buttons are in an odd layout.
If you are then pressing the back button can just pop view 3 and go back to view 2 naturally (it will still be there, so the video should still be playable) so I don't see why you need to mess with the table in view 1 in order to find that and make it show again. You should just be able to hook into viewWillAppear and do the same thing you did when coming from view 1.
And if you aren't using a standard navigation controller pattern then maybe this is the problem. From a UI perspective you probably should do that anyway - people will expect a standard back button in the title bar.
I am pretty sure your problem is soluble anyhow - you can always pass object references from one view to the other when creating them for example - but I don't think you need to resort to the method you have in mind.
edit: reading the additional detail I see you are indeed using push/pop. So you have a stack of views, and when one is popped it uncovers the one beneath it, right? It should therefore be possible to do what you want by implementing the viewWillAppear or viewDidAppear methods in your controller. When your final screen is popped, if I understand correctly it will uncover the view which is able to play the movie. Your viewcontroller will get the viewWillAppear / viewDidAppear messages in that case - by maintaining state and receiving those messages you can then implement logic to decide whether or not you need to play the movie. Rather than call the didSelectRow. method again you could perhaps put the movie play stuff in its own method so you can call from either viewDidAppear or didSelectRow...
I also am not sure that I fully understand your question either but it sounds similar to a problem I was working on recently. Check out this post on using up down arrows to move through the parent table while in a detail view. My app was based on the same sample code so you should be able to get this to work without much trouble.
Up/down arrows in nav bar of detail view to page through objects in parent table