I want to create a single person lap timer xib/class and load it twice onto a single View Controller on my Story Board. Each of the two instances will be used to time and compare two persons lap times on a single ViewController.
I have laid out two Container Views side by side within the Story Board View Controller (LapCounterViewController)
I have also created an xib and class files as a single person lap timer ( LapCounterNibViewContainer)
How do I create two instances of LapCounterNibViewContainer and put it inside each of the Container Views
_vc1 = [[LapCounterNibViewController alloc] initWithNibName:#"LapCounterNibViewController" bundle:nil];
_vc1.view.frame = self.LapCounterFrame1.frame;
//_vc1.delegate = self;
[_LapCounterFrame1 addChildViewController:_vc1];
[_vc1 didMoveToParentViewController:self];
[self.view addSubview: _vc1.view];
In the storyboard, you can add two container views to the same view controller and connect them both with the same child view controller by right-click dragging and choosing embed. This created the segue like so:
Click on the segue and give it an identifier. Then, add the prepareForSegue method to your parent view controller, and set some properties for the lap timers separately if you want.
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segueName isEqualToString: #"embedSegueToLapTimerOne"]) {
LapCounterNibViewController * childViewController = (LapCounterNibViewController *) [segue destinationViewController];
[childViewController setFoo:bar1];
}
if ([segueName isEqualToString: #"embedSegueToLapTimerTwo"]) {
LapCounterNibViewController * childViewController = (LapCounterNibViewController *) [segue destinationViewController];
[childViewController setFoo:bar2];
}
}
Related
I'm new on Stackoverflow and I'm currently learning XCode from scratch and I'm in a process of making a Single Page Application with options.
Anyone knows how to efficiently make a simple menu with multiple selectable UIButtons that make the main ViewController display different datasets depending on the selection in XCode?
Tried different things (creating SecondViewController for example but can't figure out how to pass data from it to main ViewController).
Any help will be greatly appreciated.
Refer this:
https://www.appcoda.com/storyboards-ios-tutorial-pass-data-between-view-controller-with-segue/
How to pass prepareForSegue: an object
Simple Test from above Answer Reference:
Simply grab a reference to the target view controller in prepareForSegue: method and pass any objects you need to there. Here's an example...
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Make sure your segue name in storyboard is the same as this line
if ([[segue identifier] isEqualToString:#"YOUR_SEGUE_NAME_HERE"])
{
// Get reference to the destination view controller
YourViewController *vc = [segue destinationViewController];
// Pass any objects to the view controller here, like...
[vc setMyObjectHere:object];
}
}
REVISION: You can also use performSegueWithIdentifier:sender: method to activate the transition to a new view based on a selection or button press.
For instance, consider I had two view controllers. The first contains three buttons and the second needs to know which of those buttons has been pressed before the transition. You could wire the buttons up to an IBAction in your code which uses performSegueWithIdentifier: method, like this...
//When any of my buttons are pressed, push the next view
- (IBAction)buttonPressed:(id)sender
{
[self performSegueWithIdentifier:#"MySegue" sender:sender];
}
// This will get called too before the view appears
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"MySegue"]) {
// Get destination view
SecondView *vc = [segue destinationViewController];
// Get button tag number (or do whatever you need to do here, based on your object
NSInteger tagIndex = [(UIButton *)sender tag];
// Pass the information to your destination view
[vc setSelectedButton:tagIndex];
}
}
Hope this help but please go through refernces!
I am new to IOS i created two view controller.First view controller contain tableview as table1 and Second view controller contains button. I want to pass table1 to second view controller and unhide table1 in button action.
pop up for second view controller
first view controller after button action in second view controller
For instance, consider I had two view controllers. The first contains one button and the second needs to know information on the first view . You could wire the buttons up to an IBAction in your code which uses performSegueWithIdentifier: method, like this...
// When any of my buttons are pressed, push the next view
- (IBAction)buttonPressed:(id)sender
{
[self performSegueWithIdentifier:#"MySegue" sender:sender];
}
// This will get called too before the view appears
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Make sure your segue name in storyboard is the same as this line
if ([[segue identifier] isEqualToString:#"YOUR_SEGUE_NAME_HERE"])
{
// Get reference to the destination view controller
YourViewController *vc = [segue destinationViewController];
// Pass any objects to the view controller here, like...
[vc setMyObjectHere:object];
}
}
For more information you can see this: http://www.appcoda.com/storyboards-ios-tutorial-pass-data-between-view-controller-with-segue/
My PenViewController has three labels and a Container View, which means I am using an embedded segue. The thing about embedded segue, at least per my understanding, is that they are not caused by user actions the way push segues are. But now I need my Container View to show a different child respectively when a different label is clicked. How do I pass that data to the Container View? Here is my embed segue.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"embedded_segue_to_container_vc"]) {
if ([segue.destinationViewController isKindOfClass:[BCDPenDetailContainerViewController class]]) {
BCDPenDetailContainerViewController *container = (BCDPenDetailContainerViewController *)segue.destinationViewController;
container.details=self.details;
}
}
}
The container view is just a UIView (with some IB magic thrown in), so you can create an IBOutlet to it if you need to reference it to change (or add) a child view controller. To add another child view controller, use the standard custom container controller api, and add the new controller's view to the container view,
-(IBAction)addNew:(id)sender {
UIViewController *newVC = [self.storyboard instantiateViewControllerWithIdentifier:#"NewVC"];
[self addChildViewController:newVC];
[newVC didMoveToParentViewController:self];
newVC.view.frame = self.containerView.bounds; // containerView is the IBOutlet to the container view in the storyboard
[self.containerView addSubview:newVC.view];
}
I have 3 different buttons which upon touch present the same UINavigationViewController with a container.
However, each button represents which view controller will be embed at the container.
How can I embed the necessary viewController by code?
what you can do is use an identifier which would be assigned to your various viewController as storyboardID
such as fisrtVC, SecondVC, thirdVC
the depending upon whichButton is pressed just set the identifier and use this identifier when
you want to push the controller such as
for example
while you push the navigation viewController just pass the storyboard Identifier such as
Declare a NSString *identifier;
-(IBAction)firstButtonClicked{
identifier=#"firstVC";
//pass this identifier to your navigationController
}
similarly for other Controllers
When you push the navigation controller make sure to pass this identifier along now depending upon the value you can initiate the controller on you VC as
on ViewDIdApppear:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
NSString* viewType = passedIdentifier
UIViewCOntroller* viewController = [storyboard instantiateViewControllerWithIdentifier:viewType];
Load this "viewCOntroller in your ContainerView"
You should implement prepareForSegue method:
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
id is your button that will fire segue. You do segues by drugging and drop in storyboard. Put an if statement in this method and tell your UIViewController which UIView to load in container. You can pass data like this:
// Get reference to the destination view controller
YourViewController *vc = [segue destinationViewController];
// Pass any objects to the view controller here, like...
[vc setMyObjectHere:object];
Second snippet taken from this answer.
Update.
To load different UIView in container put if statement into viewWillApper method.
This method firing earlier than viewDidLoad. If statement must check some property that tell what UIView to init. You setting up this property in prepareForSegue.
It will look like this:
if (self.viewToLoad == 1)
{
self.dynamicView = MyCustonUIViewNumberOne *view = [MyCustonUIViewNumberOne alloc] init];
}
Update 2.
Or you can do it dynamically like in this answer:
if (self.viewToLoad == 1)
{
// Replacing with your dimensions
CGRect frame = CGRectMake(x, y, width, height);
MyCustonUIViewNumberOne *dynamicView = [[MyCustonUIViewNumberOne alloc] initWithFrame:frame];
[self.container addSubview: dynamicView];
} else {
// Init other view
}
The container property:
I have a app that will use multiple UICollectionViewControllers in a container view like this:
MasterViewController > (x2) Container Views > UICollectionViewController > UICollectionView > UICollectionViewCell
I've had to do this because I need the sections(each of the collection views in my case) to be scrollable vertically. Otherwise I would used a single Collection View and use the sections to my favor. This proves to be a little tricky because so far I've had to make Master Controller the data source of each of the Collection Views. Which means I have to implement if statements to check which collection view is asking for the data. Here is a sample:
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:#"section1"]) {
self.topLeft = [segue destinationViewController];
[self setDelegate:self.topLeft]; // set topleft delegate
self.topLeft.collectionView.dataSource = self;
}
if ([[segue identifier] isEqualToString:#"section2"]) {
self.topRight = [segue destinationViewController];
self.topRight.collectionView.dataSource = self;
}
}
How can the 2 contained controllers coordinate with my master controller? Right now I don't even know why I have them. Currently I have a gesture recognizer attached to the master controller because I wanna drag and drop between Collection View Controllers. I need to constantly check which Collection View the touch is above because I want to animate items out of the way when I add one to a collection, but it seems like I have to search thru all the collection views each time the UIGestureRecognizerStateChanged is called. Is there any way I can use delegation to make everything more modular right the the master controller is doing all the work.
Thanks