Pushing view out of NSObject Class - ios

How can i push a view from a class which is a child of NSObject to the screen/window?
At this moment I'm writing a class to sync the data in my app with the data on my server and i want to use the MBProgressHUD class.
The Problem is, that the sync class is a child of NSObject and I don't know, how to push something out of this class to the screen.
Or should i have edit the NSObject class completely to another kind of parent class?
Thanks,
teawithfruit

I would present the HUD on your current view. If all the work is in your child class, use the -showWhileExecuting method and just call your child class, do all the work, then when it returns back, it will automatically dismiss the HUD.

Make a UIView property, do your graphics with that view, then, from the viewController, add the view property as a subview.

Related

iOS Swift how to monitor UIViewController lifecyle

I am new to iOS development. So pardon me if this is a very basic thing.
From what I have learnt till now:
UIViewController class resembles somewhat equivalent to an Activity class in Android.
and viewDidLoad/viewWillAppear method to onCreate/onStart method
and viewDidAppear method to onResume method
Please correct me if I am wrong here.
Now, in Android we can monitor which of these methods(including other lifecycle methods) are triggered/called by implementing an Interface (ActivityLifecycleCallbacks) (somewhat resembling a protocol in iOS) which exists in the Application class in any Activity (particularly in a class which extends Application class).
It means that now these methods will be triggered/called whenever there is any navigation from one screen to another in the android app.
How do I do this in iOS using Swift? How do I know which screen(UIViewcontroller) the user is currently in and where he is navigating?
In short I want to write a standalone class which logs which screen(UIViewController) the user is currently in and which one of the lifecycle methods(viewDidLoad, viewWillAppear etc) is being executed?
Someone please help me out.
Edit:- I don't want them to subclass my standalone class instead of UIViewController class.
There are no global events that are fired when the UIViewController lifecycle methods are called. To create those you would need to subclass UIViewController as has been suggested.
You can look at UIApplication.sharedApplication().keyWindow?.rootViewController to get the current root view controller (which is often, but not always the currently active view controller).
You wouldn't typically design an app that depended on some central object tracking the state of view controllers.
The flow of UIViewController methods is pretty well described in the class reference and you can also work it out from the function names -
viewDidLoad called after the view controller instance is loaded (once per instantiation)
viewWillAppear called before this view appears
viewDidAppear called after this view appears
viewWillDisappear called before this view disappears
viewDidDisappear called after this view disappears
Create a view controller subclass and add that implementation in there. Then make sure all the view controllers you create subclass that new class rather than UIViewController itself

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.

Send the value in Class of ViewController to Class of View

Is there any way that could send the value in Class of ViewController to Class of View?
Because I want to make a drawing board and I made a modal scene to set values about color, width .
I know how to send the value in modal scene to my ViewController , but now I need use those value in Class of View , or not Class of ViewController.
Sounds like you need to devise a protocol to open a delegation channel between the two classes. Then when the modal VC wants to send data to its delegate (its presenter, in this case), it can of its own volition.
The View Controller can have a reference to the View, therefore the View Controller can simply pass the values to View by calling a View's method or updating View's properties. This is a common pattern: View defines a Protocol and data/requests from View to ViewController go through this Protocol (the ViewController acts as delegate of View); and ViewController owns the View.
For inspiration on how to implement a farily decoupled design applying this pattern, you can have a look at documentation on UICollectionView and UICollectionViewController.
Other options, depending on what design you need, are Key-Value Observing or Notifications.

iOS : Load uiview of another view controller

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
}

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.

Resources