how to call add a view from NSObject class - iphone

I am working on an app where i am doing some code in a class which is derived from NSObject and in this class i want to call another class which is a UIView type class and add it above current view or if possible over window. Can someone suggest me how will i do it as i cannot call [self.view addsubview:view] from a NSObject type class?
Also i can not move from my current class as here i am doing act(uploading with progress view) which will take time. so i have to add subview over my current view from this class only.
and yes i have to remove that uiview also later on when uploading completes.
Thanks in advance

Sounds like you want to add a view to the window:
[[UIApplication sharedApplication].keyWindow addSubView:myView];

How about placing your NSObject class inside a UIView, and then keeping a variable that references that view (eg masterView). You could hook that up in Interface Builder to a view that contains the NSObject, or if you're not using IB, create a UIView before instantiating your NSObject-derived class inside it, and pass a reference to the NSObject class which points to the master UIView.
Then, when you want to add a new subview to the screen, call
[masterView addSubview:anotherView];

Related

I can't get my head round - (void)playInputClick;

I have an inputAccessoryView for a UITextField set up, which is loaded from a XIB when needed (in the exact same way as Apple's KeyboardAccessory example).
I'm trying to get the buttons on it to click when pressed using the playInputClick function but I can't work out how. The Apple documentation says that I need to add add a delegate method to the view, but the view was created purely in interface builder so I don't see how I can do this.
Does anyone know how to make this work? There seems to be no example code of this method being used anywhere on the internet.
I've been having the same problem and finally figured it out.
If you implement the protocol in a UIViewController it does not work. It needs to be implemented in a UIView subclass.
So to get it working, I created a view in interface builder with a view controller. Then I added another class which is a subclass of UIView. This class implements the UIInputViewAudioFeedback protocol. Then in interface builder I went to the Identity inspector of my view and changed the class to my UIView subclass which implements the UIInputViewAudioFeedback protocol. And the keyboard click sound is now working.
My view controller is still calling the [[UIDevice currentDevice] playInputClick], all I did was move the code for the UIInputViewAudioFeedback protocol into a UIView Subclass and set my views class to my UIView sublass.
Assuming you are trying to do this in an inputAccessoryView:
In the .h file, indicate that you implement the UIInputViewAudioFeedback
#interface YourAcessoryView : UIView <UIInputViewAudioFeedback>
In the .m file, add this method to satisfy the protocol
- (BOOL)enableInputClicksWhenVisible {
return YES;
}
When a button is pressed, do something like:
- (void)buttonPressed:(UIButton*)sender
{
[[UIDevice currentDevice] playInputClick];
// do more stuff
}
You need to implement a view controller class, and in the interface builder assign the files'owner as this class. Connect the view controller's view to your view.
Once you have done that you can implement the delegate in the view controller.
Its pretty simple and straight forward. Basically your view will be a member of a view controller class.

How to add a subclass of UIView to a UIScrollView?

I have a class that subclasses UIView called DrawView. This class contains custom drawing in the drawRect: method. I want to add this to a UIScrollView programmatically in a UIViewController class controlling the view which the UIScrollView has been added to. Also, I want the instance of DrawView to be a global variable.
I kind of confused and I am not really sure of anything, except that at some point I will have to call [scroller addSubview:drawViewInstance];. Thanks
Yes what you have written is correct. Also your subview should be a new class overriding the methods you need. You can maintain it globally by providing a method to return the instance more like a singleton.
Put the variable into the app delegate class. Alloc and init it and you can access it using the getter. Declare the variable in your delegate.
[UIApplication sharedApplication].delegate].globalInstance;

How to access variables of a ViewController in a subclass?

I guess this is basic, but I can't get my head around this.
I used to have only one ViewController in which all my variables were defined, e.g. an UITextView named myTextView. I also had methods in this ViewController for handling events that relate to myTextView, such as - ()hideKeyboard { // do something with myTextView or - (void)keyboardWillShow:(NSNotification *)notification { // do something with myTextView.
As my program became bigger and bigger, I thought about using subclasses, especially for other views. So I started a subclass, eg. mySubClass.h and mySubClass.m, in which I had another UITextView (for argument's sake myOtherTextView). In order to incorporate mySubClass, I #imported it into my ViewController and added a #class mySubClass; and could then produce instances of this class so as to use it in my App.
So far so good. As you can imagine, all the nice methods I defined in my ViewController for what should happen when an UITextView is edited (such as hiding keyboard etc.) didn't work for the UITextView in mySubClass.
It was then suggested to me that I should make another class in which I had all the keyboard events and subclass my ViewController and mySubView to it:
#interface ViewController : MyKeyboardEventsViewController
Now, the problem I am seeing is that I won't be able to access all the views, textviews, textfields etc. that I have created in my ViewController (e.g. myTextView which I mentioned earlier).
How can I achieve that all the variables that I have defined in my ViewController will also be available for MyKeyboardEventsViewController? Or is there another way to handle this?
Basically, I don't get how MyKeyboardEventsViewController will be able to access variables in my ViewController which it will need (e.g. the UITextView in question, or the accessoryView which will pop up etc. etc.).
Any suggestions would be very much welcome.
Example:
Class A contains a ivar UITextField textField
Class B subclasses Class A and thus it already contains ivar textField
Note: it's not the other way around. Class A does not "see" what ever is created in Class B.
When ever you subclass a class you give your new class the same ivars end methods of that subclassed class.
I hope this is what you were asking for.
EDIT
So for your example I would do the follwing:
Create a class "MyUIKeybordEventResponder"
Implement all the responder methods like - (BOOL)textFieldShouldReturn:(UITextField *)textField
Subclass your ViewController from "MyUIKeybordEventResponder"
Note method textFieldSHouldReturn has a parameter UITextField so it knows which textfield was pressed. So in a way it receives your textField from the subclass.
If I'm understanding this correctly, you have a UIViewController with MyKeyboardEventsViewController as an instance variable and you want to communicate between the two? If that is the case, one option would be to create a protocol.
#protocol MyKeyboardDelegate
- (void)closeAccessoryView;
#end
(Note - make whatever methods in the protocol that you need, this is simply an example)
In your MyKeyboardEventsViewController you then include the protocol file, and create an ivar
id <MyKeyboardDelegate> delegate;
Also make it a property and synthesize it.
Whatever class that is going to create the keyboardviewcontroller should delcare themselves as conforming to the protocol.
#interface MyViewController : UIViewController <MyKeyboardDelegate>
...
#end
When you create the MyKeyboardEventsViewController, set the delegate.
MyKeyboardEventsViewController *eventsVC = [[MyKeyboardEventsViewController alloc] init];
[eventsVC setDelegate:self];
Now just implement the delegate method and perform whatever action that is necessary.

Can i call a method inside a class(subclass of UIView) from another class?

Its kinda weird but i am not able to call any method defined inside the class (having the drawRect method).
This is what i do inside a UIviewcontroller class with nib for UI:
-(void)viewDidLoad {
[self.lineVar Start];
}
lineVar is an object of the class (which is a subclass of UIview) which has the function Start.
-(void)Start {
NSLog(#"Hi...");
}
I never see the log message.
I have assigned the Class with Start method to a view which occupies some part of the screen with the UIviewcontroller class with the nib file.
I want a method to be called everytime the view (subclass of uiview) is visited.
It only has awakeFromNib which is called just the first time. viewdidLoad would have done the trick but its not there by default.
Thanks
Make sure self.lineVar isn't nil when you call Start. If it's failing silently, that's probably the issue. You may have forgotten to connect the lineVar outlet in your NIB.

Call a function in a UIViewController's UIView

I've created a view controller and the associated view using the interface builder. I'm trying to call a function that I added to the UIView from the UIViewController. I'm not sure how to call that function though.
I've tried
[self.view myFunction]
but that just makes my program crash.
Did you declare your IB outlets and connect the method to it?
Your view (more accurately nib)'s file owner should be set to your viewController class.
Unless your calling drawing functions and methods you shouldn't call anything on the view. You just don't need to.
Edit: grammar corrections.
How is the view getting initialized? Is it a custom view type? If your view is being initialized from a nib, make sure that the class on the view type is the class that has the method implemented.
It is probably crashing because UIView does not have a method named myFunction. In order to add myFunction to a UIView object, you need to subclass it.
I assume you have subclassed your UIView, and called it something like MyView. You also probably have subclassed your UIViewController and called it MyUIViewController. Therefore you may have:
#interface MyViewController : UIViewController {
MyView *myView;
....
}
Now you can call [self.myView myFunction];
That's one way. Another way may be:
[(MyView *)self.view myFunction];
This depends on whether myView is the first view in the hierarchy (or should be set up in your NIB files).