iOS : Load uiview of another view controller - ios

I have a UIViewcontroller on which I am showing UIview which contains a registration form.
now I need this same registration form on another UIViewcontroller without recreating the same for second UIViewcontroller.
I am using UInavigationcontroller.
Is there any way to achieve this? I am stuck since last few hours and tried everything.

First method & the one that I will prefer is to create the registration form as a separate UIView class.
Then you can use it in various view controllers as subview.
Second method, you can create a parent class & inherit both the classes from this parent class. This parent class will contain all the common functionalities.(as mentioned by #alinoz).

You can add viewController in another view controller. Create instance of registretionViewController and add its view in current viewcontroller vew. [self.view addSubview:regViewController.view]. And if you want to push this you can simple create instance of it and push in navigation stack.

Let the class of your registration form view be RegisterFormView.
In appDelegate create a class public property of RegisterFormView.
#property (nonatomic,strong) RegisterFormView *formView;
When you first create form view in viewController A, assign the same in appDelegate also.
[UIApplication sharedApplication].delegate.formView = registrationFormViewInstance;
In another view controller(B) where you want to use the same formView,
if([UIApplication sharedApplication].delegate.formView != nil){
//use the form view
}

Related

PrepareForSegue between UIView and ViewController using Swift

I have a UIView in a view controller which displays an image. This UIView is associated to classX, while the view controller it is in is associated to classY.
My question is, how can I use prepareForSegue in the case? I want to send data from my viewController class to the UIView class to display an image depending on the value it receives respectively.
I have it this way because in classX I need to inherit the UIView class to display the pictures, while in classY I need to inherit the UIViewController because i have functions that need it there; from my knowledge, I cannot inherit both together.
Any thoughts on how to do this?
Thanks
Create an IBOutlet for the image view in your view controller so you can access it.
The view controller or any code in prepareForSegue can now access the image view and set its contents using whatever methods your image class provides.

MVC in iOS with custom UIViews

How do you create custom UIView with its controls in iOS??
Lets say I want Radio Streamer just with Play/Pause buttons and scroller. Also lets say I'll create new model (singleton) for that streaming.
How do I handle View and controlling? Do I create UIView and add actions (IBOutlets) to that UIView, or I need ViewController also? Or should I then just use ViewController + model without UIView? Because I saw they always say model and view should never communicate so I dont think its good idea to have just UIView and actions in it...
I have created Model (singleton) and ViewController (.xib which is view 320x50 with Play/Pause and scroller). I have implemented model and actions inside controller and now when I want to use that controller (so radio streamer) I use this code:
RadioPlayerViewController *controller = [[RadioPlayerViewController alloc] initWithNibName:#"RadioPlayerViewController" bundle:nil];
[controller.view setCenter:CGPointMake(CGRectGetMidX(self.view.bounds), 300)];
[self addChildViewController:controller];
[self.view addSubview:controller.view];
Is that good aproach?
Thanks.
way you say and implement is correct you have make model class extend with NSObject class here you create named singleton class that is fine with model class
now about view and controller in objective c this part is combine in objective c with Viewcontroller but if you want you can separated view form view controller class
what you did is perfectly fine if you need some controller with it relevant methods and need to sparted form main parent controller and need to serve as indiviual component that you can use this approch here you create RadioPlayerViewController.
you can also create custom view and than add this view in view controller
for how to create custom view you refer this link http://www.raywenderlich.com/1768/uiview-tutorial-for-ios-how-to-make-a-custom-uiview-in-ios-5-a-5-star-rating-view
this is used when you need some reusable views that used in many screens.
way you implement is good approach for custom component you can make changes in component and easily modify it with out any trouble in parent controller.

Passing a variable from modal tableview to parent view

I have a detailView which has a button on it to bring up a tableView as a modal view to help the user giving a selection of names. I'm trying to figure out a simple way of passing the selected name to a textField on the parent detailView.
I have been building this project using Storyboard, but I am totally stuck at this point.
There are several ways to approach this kind of problem:
shared object: The parent view controller provides some object to the child that contains the state that the child is expected to work with and possibly modify. In this case, it might be a dictionary that contains an array of names to display and the index of the selected person.
child's properties: Often, the child view controller itself is the shared object. The parent instantiates the child, sets some of the child's properties (e.g. people and selectedPerson), and presents it. When the child is finished, the parent gets the properties of the child that it cares about (e.g. selectedPerson) before releasing it.
delegation: Sometimes the child view controller will need to interact with the parent in order to do its job. This is a good time to use delegation: establish some protocol that the child knows about and the parent implements, add a delegate property to the child, and have the parent set the child's delegate property to itself before presenting it. This way, the child can talk to the parent without depending on the parent class.
For the case you describe, where you just want to convey some piece of data back to the parent when the child is done, the second strategy above is the simplest.
Note: DetailViewController below should be replaced with whatever the class is of your detail view controller.
1) If you don't already have a subclass of UITableViewController, create one. This will allow you to add a property to the table view controller. Change the class of your modally presented controller in your Storyboard to this new subclass.
2) Add a property to your table view controller subclass like this:
#property (weak, nonatomic) id mydelegate;
3) In prepareForSegue in your DetailViewController, set the mydelegate property of the destination view controller to self which is a pointer to your parent detailView.
4) In your DetailViewController, create a method to receive the selected name:
- (void)userDidSelectName:(NSString *)name;
5) In the tableView:didSelectRowAtIndexPath method of your tableViewController subclass, get the name from the tableViewCell and call the userDidSelectName method on the delegate:
[self.mydelegate userDidSelectName:selectedName];
6) The correct way to to this would be to define a protocol that has a method userDidSelectName and then make the delegate pointer in step 2 require a class that implements that protocol. The quicker and dirtier thing to do is to make the delegate pointer in step 2 of type DetailViewController * instead of id. This will allow you to call userDidSelectName without the compiler complaining.

Multiple 'nibs' per UIViewController

I'm using storyboards and I want two UIViewControllers to share the same controller class. I've done this and I've hooked up my UIImageViews to their IBOutlets.
When I call [day3 setImage:[UIImage imageNamed: #"1.png"]];
it doesn't change the image. Has this got anything to do with it sharing the same controller class as another view?
This view with the UIImageViews on is opened via push segue from the other view controlled by the same controller class.
Your problem is that although both views are using the same UIViewController, they are connected to different instances of them. Let's call our ViewController myViewController. When the new view is pushed it actually creates a whole new myViewController. So you now have two active in your app. One instance of myViewController for the first view and another for the second view. You can therefore not directly manipulate the views from the other view's instance of that controller.
In order to set properties you will need to communicate between the two instances of myViewController the same way you would as if they were entirely different ViewControllers. If you need further help or explanation let me know.

Unable to link UIViewController IBOutlets with StoryBoard

I have two UIViewControllers (A and B) in storyboard. The A view controller has a property
#property (nonatomic, retain) IBOutlet UIViewController *viewController;
that I want to link via storyboard to B.
The outlet shows under the the IBOutlets section in storyboard menu but I'm unable to link.
It might seem strange why I'm trying to do that but I need it. Someone knows how to do that?
IBOutlets are connections within a single view controller. Create references to objects inside the view controller so you can use those objects in code.
You cannot create IBOutlets from one view controller to another one. A property is the correct way to go, but you have to assign the property in code. Normally when one view controller creates another one, it might set a reference to itself.
OtherViewController *otherViewController = [OtherViewController alloc] init];
otherViewController.masterViewController = self;
// at this point "otherViewController" has a reference to the current view controller
Now I understand what I need to do. I need to create a custom segue to achieve the same result as when you link a UINavigationController to other ViewController and mark as RootViewController. It is done by segue and not by IBOutlet.
I am a bit late to the party however I put together a segue class to help with this job. View the git repository to see the class in action: https://github.com/datinc/DATOutletSegue.
Basically it uses the identifier of the segue to connect to the parent controller

Resources