Inserting subviews from other classes - iphone

I'm trying to add UIImageView subviews to my viewController.view, with consideration for whether they go above or below an image.
Basically, my viewController has an image of a body. I allow the user to add accessories to the scene, for example a hat. I need to put some of these images above, some below the body view index.
However I'm having trouble figuring out how to add the image behind the body. I figured I could try and create a UIImageView property as a reference point on the viewController and do something like this:
//this is from my menuItem class which has a UIViewController property connecting it to the main scene
[self.viewController.view insertSubview:sprite belowSubview:self.viewController.body];
However this code doesn't work, as I get the warning "Property body not found on object of type 'UIViewController *'", even though I #property and #synthesize it.
Does anyone know what I'm doing wrong, or have a better design on how to implement this? Thanks
#shubhank:
//.h
#interface ViewController : UIViewController {
UIImageView *body;
}
#property (nonatomic, retain) UIImageView *body;
//.m (in viewDidLoad)
self.body = [[UImageView alloc] initWithFrame:CGRectMake(250, 300, 339, 562)];
self.body.image = [UIImage imageNamed:#"body.png"];
[self.view addSubview:body];

[self.view insertSubview:sprite belowSubview:body]. Don't insert viewController after self, 'cause self IS viewController.

there is two things happening that i can't seem to understand.
[self.viewController.view insertSubview:sprite belowSubview:self.viewController.body]
Here you are accessing body by self.viewController.body
and then where body is defined
[self.view addSubview:body]
so how come you can access it both by view controller and self.view.
There is something wrong here..
Also
self.body = [[UIImageView alloc] initWithFrame:CGRectMake(250, 300, 339, 562)];
change it to
self.body = [[[UIImageView alloc] initWithFrame:CGRectMake(250, 300, 339, 562)]autorelease];
the property retains the UIImage view..you should release it.

Problem Solved:
Silly issue. I simply needed to forward declare my ViewController class in my MenuItem class, rather than use UIViewController *.
Thanks for your help.

Related

removing a subview

I have looked on here and found a few examples but nothing works so here goes...
I have a subview which is started and works fine, the camera takes a pic and then a thumbnail appears at the bottom right, when clicked I want the subview released but no matter how I try I can't get rid of the subview!
Code:
-(void)onSingleTap:(UITouch*)touch {
NSLog(#"onSingleTap");
UIImageView *eyes = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"binocs.png"]] autorelease];
eyes.tag = EYES_TAG;
[self.overlayView addSubview:eyes];
[camera takePicture];
}
Then the thumbnail action:
- (void)thumbnailTapped:(id)sender {
self.view.alpha = 99.0f;
UIButton *binocsButton = (UIButton*)[self.view viewWithTag:BINOCS_BUTTON_TAG];
binocsButton.hidden = YES;
}
So my subView is called eyes and I just need to clear that view under the thumbnail tapped action. Any ideas would be fantastic!
I'm not sure I follow entirely, but don't you just want to add this to the bottom of thumbnailTapped::
[[self.view viewWithTag:EYES_TAG] removeFromSuperview];
By the way, rather than using tags it's probably better to store a reference to the views that you want to access as an ivar of your view controller class. I would add this to your property definitions:
#property (nonatomic, retain) UIImageView *eyes;
#property (nonatomic, retain) UIButton *binocsButton;
Then synthesise them and when you create those objects, assign to the properties and then you can access them easier later. Using tags I found in the past gets quite annoying as if the view is at the bottom of a complex view hierarchy you can't access it easily. If you do do this, remember to also release them in your dealloc (unless you're using ARC).

Loading another NIB into one subview of a UIVIEW

I have a simple question that I couldn't see it answered on the whole site,
One XIB file that has a UIView as the main,
in it has another UIView that's connected to one IBoutlet so I can assign it later to load a view from another XIB file. That assignment doesn't work.. it remains blank...
#interface Subclass : UIViewController {
UIView *view1;
}
#property (nonatomic,retain) IBOutlet UIView *view1;
#end
i linked view1 to the subview in the xib file
in implementation in initWithNibName I'm loading Other XIB file and use it's view and assigning it to the view1. Still this doesn't work...
detailControler = [[XibViewControler alloc] initWithNibName:#"XibViewControler" bundle:nil];
//one aproach
[self.view1 addSubview: detailControler.view];
//another aproach
[self setView1:detailControler.view];
If I'm adding the subview programaticaly to [self.view addSubview:detailControler.view] and set a frame to it, it will go fullscreen, and hide the original view.
I also had a different approach that didn't work
How to load multiple views on each button tap when using a UISegmentedVIew
This is how I usually set up a UIView inside another view, although I'm not entirely sure if it's best practice or anything like that:
if(newViewController == nil){
newViewController = [[NewViewController alloc] initWithCoder:nil];
}
newViewController.view.autoresizingMask = UIViewAutoresizingNone;
if([newViewController.view superview] == nil){
[view1 addSubview:newViewController.view];
}
Hope that Helps!

How to reference a subview without outlet

Just beginning with iPhone development i seem to miss something fundamental.
In a View based application i'm adding programaticaly a UIView subclass in the ViewController implementation file and can set a value:
- (void)viewDidLoad {
[super viewDidLoad];
CGRect myRect = CGRectMake(20, 50, 250, 320);
GraphView *graphView = [[GraphView alloc] initWithFrame:myRect];
[self.view addSubview:graphView];
graphView.myString = #"Working here";
}
When i try to change the same value with an action in the same file, the Build fails because graphView is undeclared:
- (void)puschButton1 {
graphView.myString = #"Not working here";
}
Because I use a UIView subclass there is no outlet for my GraphView instance.
How can i get a reference to my subview? Or should this be done in another way?
Thanks in advance
Frank
The easiest solution is to make graphView an instance variable for your view controller, instead of declaring it in viewDidLoad.
Put something like this in your .h file:
#class GraphView;
#interface MyViewController : UIViewController {
GraphView *graphView;
}
// ... method declarations ...
#end
If you don't want to do that, another way is to set graphView's tag property, and then call the superview's viewWithTag: method to retrieve the view when you need it.
I'm not sure what you mean by "Because I use a UIView subclass there is no outlet for my GraphView instance." You generally declare outlets on your controller class, and it doesn't matter whether you are using a subclass of UIView.
As an aside, I'll note that you should probably release that GraphView at some point, or you'll have a memory leak.
You could store graphView as a class variable.
EDIT: note that addSubview will increase the retain count of the object, somewhere in your class you will need to balance the alloc with a release and the addSubview with a removeFromSuperview or a release.

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

Multiple Views in One Nib (addSubview)

I'm trying to keep my code clean and keep the number of files down. I am using a UITableViewController and I would like to load another view on top of it. I thought this would be pretty simple:
(1) create an IBOutlet in my .h file
#interface MyViewController : UITableViewController {
...
UIView *downloadView;
...
}
...
#property (nonatomic, retain) IBOutlet UIView *downloadView;
...
(2) link it to my view in IB
(3) do something like:
self.view = downloadView;
or
[self.view addSubview:self.downloadView];
But that doesn't work for me. If I do
[self.tableView removeFromSuperview];
Then the table view goes away but I don't know how to add my view from my nib. I'm doing everything for tableView programatically but I didn't think that would matter. And with UITableViewController subclassing UIViewController I thought it would be no problem using addSubview or something like that. What am I missing? This shouldn't be that hard right?
Update:
So if I do the following:
UIView *newView = [[UIView alloc] initWithFrame:self.view.frame];
newView.backgroundColor = [UIColor greenColor];
[self.view addSubview:newView];
It does (mostly) what I want. So does this mean I'm messing something up with the way I'm connecting my view from IB? In IB I set the fileOwner's class to MyViewController and I make a connection between downloadView and the view that I created (in IB). That's all I should have to do right?
I think it's an issue of where in the hierarchy you're adding the view. You can try this:
[UIWindow addSubview:self.downloadView];
and see if it appears. Or perhaps,
[self.tableView addSubview:self.downloadView];
Otherwise I think you have the right idea.