removing a subview - iphone

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).

Related

Inserting subviews from other classes

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.

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 use same UIView in tabs?

i have a small UIView, i fill it with some string data from net.
i want to it load just once. thats why i call it in appdelegate class.
CurrencyView *currView;
#property (nonatomic, retain)CurrencyView *currView;
in application didFinishLaunchingWithOptions:
CurrencyView *view=[[CurrencyView alloc] initWithFrame:CGRectMake(0, 0, 100, 20)];
self.currView = view;
[view release];
then i call it in every viewcontroller (in tabs).
AppDelegate_iPhone *delegate = [AppDelegate_iPhone sharedAppDelegate];
self.currencyView= delegate.currView
[self.view addSubview: currencyView];
when i move in tabs, currView moves from tab to other tab. when look back to the previous tab, currencyview is gone. it stays on the last tab. i dont know why.
what should i do for putting currview in all views?
You can't put an UIView to different controller at the same moment. When you add it to another controller / view, it will be removed from is previous one.
So you should re-add it when you change of tab ^^ (by using a delegate method, for example. It depends what you are using ;-))
I think there isn't another solution but if someone has a better idea it can interest me.
If you want to share the same view instance, you can retrieve the instance from the appdelegate in the viewcontroller viewWillAppear: method, check if the view's superview is not nil and eventually remove it from the superview, then add it to the current view. Something like this
-(void)viewWillAppear {
AppDelegate_iPhone *delegate = [AppDelegate_iPhone sharedAppDelegate];
CurrencyView *cView = [delegate currView];
if(nil!=[cView superview) {
[cView removeFromSuperview];
}
[[self view]addSubview:cView];
}
Maybe you can remove the currView from the superview in the viewWillDisappear: method rather than in the above.
Still, this remain very error prone and not very clean. It would be better if you just allocate two instance of CurrencyView and let them share the common data/model.
If you are low on memory, I doubt a single view will kill you app, but if that's the case there are other way to reduce the memory footprint, see the Apple's documentation on memory management and view controller lifecycle.
If what you want is a copy of the view, your #property statement for *cView should be
#Property (nonatomic, copy) UIView* cView;
if what you have instead is
#Property (nonatomic, retain) UIView* cView;
You are trying to share the same object in multiple views, which you can't do. With the former each time you grab it from the superview you will be making a copy of it, and I think that will work.

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.