How to call a UIView from a UIViewController - iphone

I need to create a transparent UIView with an UIActivityIndicator and an UIImage on it.
I found a code from a tutorial as in how to create the Round edge UIView.
Can someone help me call this UIView from my UIViewController class. (To call a UIViewConroller from another UIViewController we use the following code;)
ViewOne *v1 = [ViewOne alloc] initWithNibName:nil bundle:nil];
[self.navigationController v1 animated:YES];
but this doesn't work when calling UIView's. So how can i call it ?
To add an UIImage and a UIActivity indicator programatically to the UIView ?

Every view controller has a view property. Just use UIView *yourView = yourViewController.view to get it. From there you can use [yourView addSubView:someOtherView] to add the activity indicator and the image, as well as setting the transparency.

wouldn't you just wire up the UIView to an IBOutlet, and then just add your programmatically created view to the outlet

Related

UIButton in Subview calling properties

It's a little bit to explain why I'd like to implement this design.
But the question I want to ask is : If I have a view and it's controller is ControllerA, and now I want to add a subview in my that view suppose View1. And that subview View1 contains a button, which I set the IBAction point to the ControllerA.
But I found that this View1 button cannot change some properties in original view's object like UIImageview.hidden.
Can't a button in subview alter things inside superview ? Or I need to set up other things to finish this task.
i guess you were try to add a button inside the UIView which is also a subview of View(UIViewController).
as you told whenever you try to access your Button property then you could not do same.
this is happening because whenevre we create anything through the XIb if we want access that UIControl or whatever then we have make Reference of that in Our Source Code.so you'll have hook up the UIButton with reference from the Xcode.
As i am doing in below Image.
I hope i got your Point. it'll be helpful to you
It's hard to understand exactly what you're saying but it sounds like you're trying to access a subview of a UIView instance outside of that UIView. If it is not a UIView subclass where you have an ivar/property reference to that subview, you can try giving it a tag and accessing it that way.
example:
UIView *topLevelView = [[UIView alloc] init];
UIView *viewA = [[UIView alloc] init];
UIView *otherView = [[UIView alloc] init];
otherView.tag = 5;
[topLevelView addSubview:viewA];
[topLevelView addSubview:otherView];
UIView *viewASubview = [[UIView alloc] init];
[viewA addSubview:viewASubview];
UIView *referenceToOtherView = [viewASubview.superview.superview viewWithTag:5];
UIView *anotherReferenceToOtherView = [topLevelView viewWithTag:5];

Add subview to another view

With two ViewControllers, MyView 1 and MyView 2, is there possible to add a subview to MyView2 from MyView1.m?
I have tried:
MyView2 * screen = [[MyView2 alloc]initWithNibName:nil bundle:nil];
[screen.view addSubView:mySubView];
But my new instance of MyView 2 has no connection to the 'visible' ViewController on MyView2, right?
To clarify, the ViewController that is showing, is MyView1. I want MyView1 to be able to add a subview to the MyView2 view.
Thanks
I think you're confusing viewControllers with views, or at least your question is. Maybe it's something like this you're looking for -
MyViewController2 *myViewController2 = [[[MyViewController2 alloc] initWithNibName:nil bundle:nil] autorelease];
[myViewController2.view addSubView:mySubView];
// add any other views to myViewController2's view
[self.view addSubView:myViewController2.view]; // adding the view to VC1's view
If you want to be able to continue adding stuff throughout MyViewController1, you should declare either myViewController2 or its view as a retained property.
You could have MyView1 controller save some information in a common object in your app so that when MyView2 reappears it can add the subview to its view if needed.
Assuming MyView2 is a subclass of UIViewController, there is no addSubView: method on the viewcontroller itself. Instead, you want to add the subView to your view controller's view, like this:
[screen.view addSubview:mySubView];
try this,
[MyView1.view addSubView:MyView2];
[self.view addSubview:MyView1];

iOS4 - custom uiview accessing UINavigation stack

Bit confused with this one so bear with me...
I have a Navigation-based project which is working fine. I'm trying to create my first custom UIView to make a couple of buttons which I will use in multiple places. One of the buttons needs to push a viewcontroller into the navigation when it's clicked but I'm not sure how to do this.
When I had the button set up within a view controller I was using:
LocationViewController *controller = [[LocationViewController alloc] initWithNibName:#"LocationViewController" bundle:nil];
[self.navigationController pushViewController:controller animated:YES];
[controller release];
but the self.navigation controller won't work now, will it? How do I access the navigation controller of the viewcontroller that this uiview will be added to?
Hope at least some of that makes sense, as I said it's my first go at subclassing the uiview and adding it to multiple pages so I'm a bit lost.
EDIT TO ADD - I have the button click events inside the custom UIView, so that is where I'm trying to change the viewcontroller from. Should I instead wire up the events in whichever viewcontroller I add the view to?
Usually your appDelegate has a UINavigationController property. You can access it in your custom view like this:
UINavigationController *navController = (MyAppDelegate *)[[[UIApplication sharedApplication]
delegate] navigationController];
But more effective way is to make delegate method for your custom view and handle button action in your viewController.
MyCustomView.h
#protocol MyCustomViewDelegate
#interface MyCustomView : UIView {
id<MyCustomViewDelegate> cvDelegate; }
#property(nonatomic, assign) id<MyCustomViewDelegate> cvDelegate;
#protocol MyCustomViewDelegate #optional
-(void)didClickInCustomView:(MyCustomViewDelegate*)view withData:(NSObject*)data;
#end
MyCustomView.m
- (void)myButtonClick:(id)sender
{
[self.cvDelegate didClickInCustomView:self withData:someData];
}
So now you can handle this event in any place where is your custom
view.
Add the button from the interface builder or from the view controller's viewDidLoad using code:
CGRect frame = CGRectMake(0, 0, 24, 24);
UIButton *button = [[UIButton alloc] initWithFrame:frame];
[button addTarget:self action:#selector(handleMyButton:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
Then implement -(void)handleMyButton:(id)sender {}; in your view controller. Or you could instead write -(IBAction)handleMyButton:(id)sender {}; and link method and button using the interface builder.
Then inside the method just paste the block of code you posted above. If you started with the Xcode navigation controller template project it should work.
I think it's cleaner to hide the designated initializer initWithNibName: because it is an implementation detail.
When you say you are subclassing the UIView I don't know exactly what you mean. If you want to add another view controller with a custom view just use the UIViewController template and customize the XIB file, no need to subclass an UIView unless you are really modifying its behaviour, which I guess you are not. The view is a view, and the controller stuff like handling buttons should be in the controller.
The actual controller need to be in the navigation controller stack to be able to push another controller.
Or you can make a new navigation controller instance and push your LocationViewController.

TouchesBegan on a Sub-ViewController not getting called

I have a ViewController that responds to some touchEvents (touchesBegan, touchesMoved, etc...).
I've found that when I show this controller using presentModalViewController: it works just fine, but I'm trying to add it's View as a subview of another ParentViewController like so:
- (void)viewDidLoad
{
[super viewDidLoad];
//Add SubController
controller = [[SubViewController alloc] initWithNibName:#"SubViewController" bundle:nil];
controller.view.frame = CGRectMake(0, 30, 300, 130);
[view addSubview:controller.view];
[controller release];
}
When I do this, it gets added the parent view but it no longer responds to touch events. Is there a way to fix this?
Also, is there a better way to go about this? I know I probably could have used a View subclass for the child view, but it's supposed to use a Nib and I wasn't sure how to handle that without using a ViewController.
You're correct you should use a UIView subclass.
The easiest way to load it from a nib is to include the subview in your nib.
Just drop a UIView into the view connected to the original view controller.
Then with the view inside selected go to the identity inspector. It's the one that looks like a little ID card.
The very first field is called Custom Class.
Type the name of your UIView subclass here.
If you need a reference to this just create an IBOutlet in your original view controller and hook it up. That way you can set hidden = YES until you need it.
In your UIView subclass you might want to override
- (void)awakeFromNib
This will get called when the nib first unpacks.
for setting up any gesture recognizers, etc.
To load a nib directly into a view :
// Get the views created inside this xib
NSArray *views = [NSBundle mainBundle] loadNibNamed:#"myViewNib" owner:nil];
// There's probably only one in there, lets get it
UIView *myView = [views objectAtIndex:0];
// Do stuff . . .
[[self view] addSubview:myView];
You could try to call becomeFirstResponder in your subview and see whether it receives touchesBegan... It is probably so, but it will also possibly make the superview not receive touchesBegan if you require it...

Sharing image between two view

I am trying to learn something basic and very important to develop decent iPhone apps. But I don't know enough to understand what I am not getting. Here's the question:
I have a window project, with 2 views - View1, View2. On each view (thru IB) I dropped an imageView control. When I call a function in view1 I want to set the image control (show an image) of View2.
How do I do that?
There must be a simple way but I haven't managed (and did search a lot) to find a straightforward simple way to do it or at least understand the concept.
Thanks in advance.
-mE
You don't necessarily need to share the image. Is it large? Are you downloading it? In other words, does it really need to be shared?
If it does, you can just instantiate the UIImage in your app delegate and pass it to each view controller when they are created. Set a property for each view controller for the image. Something like this:
ViewController1 *controller1 = [[ViewController1 alloc init];
[controller1 setImage:image];
[[self navigationController] pushViewController:controller1];
[controller1 release];
ViewController2 *controller2 = [[ViewController2 alloc init];
[controller2 setImage:image];
[[self navigationController] pushViewController:controller2];
[controller2 release];
Then in each view controller's viewDidLoad, you can set the image for your imageView's
- (void)viewDidLoad;
{
[super viewDidLoad];
[[self imageView] setImage:[self image]];
}
I'm not sure how you're a loading your view controllers, but the above code assumes you are using a navigation controller stack. If you need clarification for a different approach, let me know.
You should connect the UIImageViews in IB to outlets in your ViewController class. In your class's .h interface file, add as appropriate:
IBOutlet UIImageView *view1;
add a property:
#property (nonatomic,retain) IBOutlet UIImageView *view1;
and in your class implementation file (.m), synthesize the property:
#synthesize view1;
Finally, you need to connect the UIImageView object in IB to that new outlet. Then you can access the image property of the UIView within your class with view1.image