removeFromSuperview not removing button from the view - iphone

removeFromSuperview not working Properly?
I added one button over another button.
When i try to remove the latter button from the view using removeFromSuperview function call , it does not worked.

the following Code works for me perfectly;
header file
#interface ViewController : UIViewController
{
UIButton *btnShadow;
}
#property (nonatomic,retain) UIButton *btnShadow;
implementation
#synthesize btnShadow;
-(void) vDrawGrayView
{
btnShadow = [[UIButton alloc] initWithFrame:CGRectMake(0, 44, 320, 416)];
btnShadow.backgroundColor = [UIColor colorWithRed:((CGFloat)79/255) green:((CGFloat)73/255) blue:((CGFloat)73/255) alpha:1];
[btnShadow addTarget:self action:#selector(HideKeyboard) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btnShadow];
}
whenever you need to remove the button use:
[btnShadow removeFromSuperview];
note
Dont fotget to release the button and make sure you are removing the button that is on the front, you can make it in the front of the UIView by using:
[self.view bringSubviewToFront:btnShadow];
Good luck.

Related

When addSubview and release not working properly in added view

I'm created Class and it's add to the current view with non arc project. After that i'm releasing it as this.
TestViewController *tView=[[TestViewController alloc] initWithNibName:#"TestViewController" bundle:nil];
tView.view.frame=CGRectMake(10, 10,tView.view.frame.size.width , tView.view.frame.size.height);
[self.view addSubview:tView.view];
[tView release];
I added button to the TestViewController and when pressed it just crash and view this message from console.
-[TestViewController performSelector:withObject:withObject:]: message sent to deallocated instance
Anyone can explain the reason for that?
When you call [tView release]; TestViewController's dealloc method will automatically get called. And Objects of this class will be released. So Probably you have released that button in dealloc. That's why your app is crashing.
This is not the right way to do it.
You should create a custom view and add that view to self.view instead of adding viewcontroller's view.
Currently, you had declared TestViewController instance as local. So, that only it is getting crash while accessing the controls which are there in the instance.
Declare TestViewController instance in class level(ivar) and then use it.
Obviously, the target of your button is tView. [tView dealloc] is called after [tView release] as its retainCount decrease to 0.
You should declare tView as a private member variable, such as _tView, and call [_tView release] in your view controller's dealloc function.
#interface **
{
TestViewController *_tView;
}
if(!_tView){
_tView=[[TestViewController alloc] initWithNibName:#"TestViewController" bundle:nil];
}
_tView.view.frame=CGRectMake(10, 10,tView.view.frame.size.width , _tView.view.frame.size.height);
[self.view addSubview:_tView.view];
In iOS 5.*, custom container view controllers is supported. (http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/CreatingCustomContainerViewControllers/CreatingCustomContainerViewControllers.html )
You can write code like this:
TestViewController *tView=[[TestViewController alloc] initWithNibName:#"TestViewController" bundle:nil];
tView.view.frame=CGRectMake(10, 10,tView.view.frame.size.width , tView.view.frame.size.height);
[self.view addSubview:tView.view];
[self addChildViewController:tView];
[tView didMoveToParentViewController:self];
[tView release];
you can use below code
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:#selector(aMethod:)
forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[self.yourViewController addSubview:button];
self.viewController means you have define your viewcontroller in .h file and then use you instance of the view controller to add your button.
then you can release your viewController [ViewController Release];

UIButtons not working on programmatically created UIView

I have a UIViewController which I want to display a UIView that renders as a menu. This menu will have several buttons on it. I wanted to reuse this menu a few different places in my app so I figured I would create a class called ViewFactory that has a method that returns a UIView with these buttons.
On my ViewController I call this method and get the returned UIView and add it as a subview.
This works just fine. I can see the view and all its buttons, however, the buttons do not respond to any touch events. Not sure why this is the case and curious to know what I am doing wrong.
Here is my code for the ViewFactoryClass:
- (UIView *) addCloseRow
{
// UIView container for everything else.
UIView *navRow = [[UIView alloc] initWithFrame:CGRectMake(0,225,350,45)];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.userInteractionEnabled = YES;
[navRow addSubview:button];
[button addTarget:self action:#selector(closeButtonTouchDownEvent) forControlEvents: UIControlEventTouchDown];
navRow.userInteractionEnabled = YES;
return navRow;
}
In my main NavigationController class here is how I am calling and getting the UIView:
ViewFactory *factory = [[ViewFactory alloc] init];
[self.navigationController.navigationBar addSubview:[factory MainNavigationUIView]];
Again, the UIView shows up but the buttons never respond to anything.
You added the button with target and selector for ViewFactoryClass
And now you are creating instance and trying to call an action from ViewFactory class.
You can change the method to something like this:
- (UIView *) addCloseRow : (id)object {
...
[button addTarget:[object class] action:#selector(closeButtonTouchDownEvent) forControlEvents: UIControlEventTouchDown];
...
}

Two buttons in two difference views, acting as a single button

I work on a project for iPhone iOS4 with Xcode 4.
My app uses a tabBar for two Views with two View Controllers.
I want to programmatically create a Button in a View and to have same button in the other view.
For "same button" I mean that buttons have same background Image, same Title and so on. Also, when I programmatically change first button title also second button title change; same for backgrounds.
I was thinking something like "passing the pointer", but I do not know how to do it, how to pass a pointer from a View to another View. (I have a singleton GlobalData, if it can help.)
Thank you.
What you want to do is to create a custom UIButton, and then just use it wherever you need it. Once you change it in it's implementation file it will change globally.
Example CustomButton
//CustomButton.h
#import <UIKit/UIKit.h>
#interface CustomButton : UIButton{
}
#end
//CustomButton.m
#import "CustomButton.h"
#implementation CustomButton
- (id)init
{
self = [super init];
if (self) {
self.type = UIButtonTypeCustom;
self.frame = CGRectMake(170, 45, 150, 40);
[self setTitle:#"Title" forState:UIControlStateNormal];
[self.titleLabel setFont:[UIFont fontWithName:#"Helvetica-Bold" size:15]];
[self setBackgroundImage:[UIImage imageNamed:#"bg_image.png"] forState:UIControlStateNormal];
}
return self;
}
#end
Then use it like so:
#import "CustomButton.h"
...
CustomButton *myButton = [[CustomButton alloc] init];
Although the approach looks a bit shady, but I do not know what the use cases are so here it goes.
You can create a UIButton subclass and make that a singleton. Or store that in the AppDelegate.
An interesting thing to note is that when you add the same object to a second view, it will be removed from the first view! So you will have to keep adding it back to the view when ViewController's viewWillAppear: method is called.

UIScrollView crashes app when touched

I am running into a very annoying problem: I am creating an UIScrollView that containes an UIView that contains some buttons. The UIView with buttons work fine. But the UIScrollView, no matter what I do with it, when touched, crashes. It doesn't make any difference it's empty or not. It keeps crashing.
I am very lost and don't know what else to try.
Thanks very much.
In the viewController.h
#interface tagstestViewController : UIViewController <UIScrollViewDelegate> {
UIScrollView *scrollViewContainer;
}
#property (nonatomic, retain) UIScrollView *scrollViewContainer;
In the viewController.m:
UIScrollView *scv = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0, 320, 200)];
scv.backgroundColor = [UIColor blackColor];
scv.autoresizingMask = UIViewAutoresizingFlexibleHeight;
scv.bounces = YES;
scv.scrollEnabled = YES;
scv.clipsToBounds = YES;
scv.delegate = self;
[self setScrollViewContainer:scv];
[scv release];
[self.view addSubview:scrollViewContainer];
Are you releasing your viewController right after you add its view into the view hierarachy? Because if you do that, I noticed that even though the application keeps running fine, if you use "scv.delegate = self", it will crash when it tries to deliver the delegated messages to the viewController.

Create UIView through code and add events in iphone

I have created a UIView and added label to it and latter assign it to Controller.
Now whenever I click on my View it shows me "EXC_BAD_ACCESS”.
Below is my code.
//create a UIView in App Delegate
UIView *viewPtr = [[[UIView alloc] initWithFrame:frmRect] autorelease];
//created a Button and added to UIView
UIButton *btnPointer = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btnPointer.frame = cgframe; // provides both a position and a size
[btnPointer setTitle:btnLabelText forState:UIControlStateNormal];
[btnPointer addTarget:self action:#selector(generate:) forControlEvents:UIControlEventTouchUpInside];
[viewPtr addSubview:btnPointer];
//Now need to add this UIView to a controller
viewController.view = viewPtr;
I am able to display the button on the Form but when I click on the form or the button I get
"EXC_BAD_ACCESS”.
You should create the view in the .m file of your view controller like this.
- (void)loadView
{
UIView *viewPtr = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
//created a Button and added to UIView
UIButton *btnPointer = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btnPointer.frame = cgframe; // provides both a position and a size
[btnPointer setTitle:btnLabelText forState:UIControlStateNormal];
[btnPointer addTarget:self action:#selector(generate:) forControlEvents:UIControlEventTouchUpInside];
[viewPtr addSubview:btnPointer];
//Now need to add this UIView to a controller
self.view = viewPtr;
[viewPtr release];
}
Hope this helps.
Thanks,
Madhup
Instead of use the autorelease
UIView *viewPtr = [[[UIView alloc] initWithFrame:frmRect] autorelease];
Create the UIView *viewPtr on the .h file, and only do the release in the dealloc method
So, you will have the declaration on your .h file
UIView *viewPtr;
And you .m file will be with the following lines, when you instantiate the viewPtr in the same place where you was doing previously, but without the autorelease, and the release on the dealloc method as follow:
viewPtr = [[UIView alloc] initWithFrame:frmRect];
.
.
.
.
.
.
.
.
- (void)dealloc {
[viewPtr release];
[super dealloc];
}
The autorelease is the main problem in your code, because when you do this, your UIView won't respond to any event.
Cheers,
VFN
What is self in [btnPointer addTarget:self ... ? Is generate: called? Have you tried debugging there?
As vfn says, this you get the error because you release the view too early. The question I would be asking is why is this case different?
Normally in an assignment like this:
viewController.view = viewPtr;
The viewPtr is retained and you would be correct to release it yourself. But look at the definition:
#property(nonatomic, retain) UIView *view
This means that any value is simply assigned and not automatically retained.