How to remove a subview if triggered from one of its components? - iphone

I'm looking for a smart way to remove a subview (with removeFromSuperview) when the subview itself (or precisely said one of its components) triggered the removal. As for the source code this would be like
UIView * sub_view = [[[UIView alloc] initWith...
UIButton *button = [UIButton buttonWithType...
[sub_view addSubview:button];
[self.view addSubview:sub_view];
If the button have now something like
[button addTarget:self action:#selector(closeMySubview) forControlEvents:UIControlEventTouchUpInside];
the call to removeFromSuperview inside closeMySubview does not work but results in SIGABRT and unrecognized selector sent to instance ... . Well that there is something not more present anymore is not a surprise but what would be the right way?
(Removing the subview if triggered from an another gui component would work of cause but is not the point here.)

The best pattern for this type of action is the "delegate" pattern.
You can subclass anything and add this property:
#property (assign) id delegate;
for the instance variable:
id delegate;
Also, define a protocol like this:
#protocol MySubViewDelegate
-(void)myViewDidFinish:(UIView *)view;
So in your view controller, you can instantiate the subview, tell it your its delegate, and add it to the view. Then, an action on the subview calls the method:
[delegate myViewDidFinish:self];
The viewcontroller then can say something like:
[view removeFromSuperView];

It should work.
Check if your sub-view points to an existing view.
Post the entire code of the initiation and the removeFromSuperview.

Related

How do I use a subclass instead of intended class?

I have a class which includes a UIButton as a subview. I have a subclass of UIButton which I've made, which has my own modifications to it, and I want to use this in place of the UIButton, but I don't want to just remove the original UIButton and add this one on instead, because then it wouldn't respond to the UIButton commands that the class gives to it.
How can I use my subclass of UIButton here?
Hope this makes sense.
EDIT: So I have a subclass of UIView. It's called MyView. MyView has a subview of a UIButton. I also have a subclass of UIButton, called MyButton. I want to use MyButton instead of UIButton on MyView. How can I do this?
At top of header or implementation file:
#import "MyView.h"
#import "MyButton.h"
Where you need it:
MyView *myView = [[MyView alloc] init];
MyButton *myButton = [[MyButton alloc] init];
[myView addSubview:myButton];
Simply as that, of course setting the button to be at the right place, having the right size, etc, I leave up to you.
If you are subclassing UIButton just to add properties or methods then you should create a category instead. If you are creating a custom button that needs to override existing methods or perform additional drawing then you just need to create an instance of your button and add it to your view.
MyButton *button = [[MyButton alloc] initWithFrame:CGRectMake(10, 10, 100, 100)];

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

Accessing Button Created in Subclassed UITabBarController

Ok, so I'm a little stuck and maybe someone can lend some advice.
I've subclassed UITabBarController, and am creating a custom button that overlays the tab bar whenever viewDidLoad gets called inside the CustomTabBarController.
This works great, except its not tied to any action.
What I would like to do is to have a UIModalViewController be displayed when that button is pressed. Now, preferably I would rather not make this call from the subclassed CustomTabBarController, but rather from within one of my viewControllers (rootViewController per-say) that is associated with a tab.
Can someone direct me in how to make this happen? IE, How to instantiate a button in one class and make that button respond to an action within another class.
Should I use NSNotificationCenter, delegate responders, something else? An example would be great :)
There are several approaches to achieve what you're asking for. The approach I usually take is that I do something like this:
// CustomTabBarController.h
#protocol CustomTabBarControllerDelegate
- (void)buttonAction:(id)sender;
#end
#interface CustomTabBarController : UITabBarController {
id<CustomTabBarControllerDelegate> customDelegate;
}
#property(nonatomic, assign) id<CustomTabBarControllerDelegate> customDelegate;
#end
// CustomTabBarController.m
#interface CustomTabBarController ()
- (void)buttonAction:(id)sender;
#end
#implementation CustomTabBarController
#synthesize customDelegate;
- (void)viewDidLoad {
[super viewDidLoad];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
// Configuration of button with title and style is left out
[button addTarget:self action:#selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
- (void)buttonAction:(id)sender {
[self.customDelegate buttonAction:sender];
}
#end
I don't know what you're button will be doing, so I just call the method buttonAction:. Since UITabBarController already has a property named delegate I called our delegate customDelegate. All you need to do to make the above work is to add the following line to your root view controller (or whatever controller you want to handle the button action).
customTabBar.customDelegate = self;
Of course you also have to implement the protocol.
One could also imagine not using a delegate and just set the target like this:
[button addTarget:self.rootViewController action:#selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
The above code assumes that customTabBarController has a rootViewController property and that it has been set. Also it assumes that the root view controller has the following method:
- (void)buttonAction:(id)sender;
I prefer the delegate approach as it is the more general approach, but the later approach will also work. Using NSNotificationCenter is also an option, but I'm personally not a fan of sending notifications when it isn't necessary. I usually only use notifications when multiple objects need to respond to an event.
You can reference all the view controller in your UITabBarController with the viewControllers array. You can easily get view controller for the currently selected view with selectedViewController.
With that in mind, your CustomTabBarController action can call a methods on these view controllers. Just add method to the appropriate view controller(s) to display your UIModalViewController.

How to Instantiate and Call UIView Subclasses Without Using a Nib

Without using Interface builder or xib files, what is the correct way to instantiate two classes which inherit from UIView such that they can switch between themselves using UIButtons located on the views themselves?
I think this involves setting up a UIViewController from the app delegate and adding two instances of my classes which implement UIView into the controller (perhaps from inside the controller?)
I'm also not sure how to raise events from UIButtons on the custom UIViews to switch the views. I suspect I would need to add a method to the view controller but I'm not sure how to get a reference to the view controller from inside the scope of my UIView.
Also, I'm wondering that,if the use of a UIViewController is necessary, should the switch method could be in the scope of the main app delegate?
Some code examples would be great!
Your main problem is that you don't conceptually understand the role of UIViewControllers versus UIViews. Most people don't when they first start out.
Views are stupid and ideally, they should be composed of generic objects. They contain virtually none of the logic of the interface. They do not know or care about the existence of other views. The only logic you put in views is logic that pertains to the immediate and generic functioning of the view itself, regardless of the data it displays or the state of other parts of the app. You seldom need to subclass UIView. This is why views can be completely configured in Interface builder without any code.
ViewControllers contain the logic of the interface and connect the interface to the data (but they do not contain or logically manipulate the data.) They are "intelligent" and highly customized. The viewControllers do understand the place of the view in the context of the app. The viewControllers load and configure the views either from nib or programmatically. The viewControllers control when the views are displayed or hidden and it what order. The viewControllers determine what action is taken in response to events and what data gets displayed where.
VictorB's example code shows how this is all done pragmatically. The important thing to note is that the viewController and view are entirely separate objects from two entirely separate classes. There is no overlap and no need to subclass UIView. All the customization is in the controller.
All this is because of the MVC design patter. It decouples the interface from the data model, making them both modular and independent of each other. This makes it easy to design, debug, and reuse each independent module.
If you want to get it done in code, here is an example I just drummed up using lazy loaded UI elements. I'm only making one button here and swapping it between whichever view is active. It's slightly awkward, but it reduces the amount of code necessary to demonstrate this.
I've created two UIViews to represent your custom classes, one with a blue background and one with a red. The button swaps between the two. If you have a unique button already in each of your custom views, you just need to either expose those buttons as properties of your UIView subclasses so your view controller can access them, or add the view controller as a target for the button's action from within your UIView's loading code.
I've tested this code in my simulator and it seems to work fine, but you really should try to understand what's going on here so you can implement it yourself.
ToggleViewController.h:
#import <UIKit/UIKit.h>
#interface ToggleViewController : UIViewController {
UIView *firstView;
UIView *secondView;
UIButton *button;
}
- (void)addButton;
- (void)toggleViews;
#property (nonatomic, readonly) UIView* firstView;
#property (nonatomic, readonly) UIView* secondView;
#property (nonatomic, readonly) UIButton* button;
#end
ToggleViewController.m:
#import "ToggleViewController.h"
#implementation ToggleViewController
// assign view to view controller
- (void)loadView {
self.view = self.firstView;
}
// make sure button is added when view is shown
- (void)viewWillAppear:(BOOL)animated {
[self addButton];
}
// add the button to the center of the view
- (void)addButton {
[self.view addSubview:self.button];
button.frame = CGRectMake(0,0,150,44);
button.center = self.view.center;
}
// to toggle views, remove button from old view, swap views, then add button again
- (void)toggleViews {
[self.button removeFromSuperview];
self.view = (self.view == self.firstView) ? self.secondView : self.firstView;
[self addButton];
}
// generate first view on access
- (UIView *)firstView {
if (firstView == nil) {
firstView = [[UIView alloc] initWithFrame:CGRectZero];
firstView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
firstView.backgroundColor = [UIColor redColor];
}
return firstView;
}
// generate second view on access
- (UIView *)secondView {
if (secondView == nil) {
secondView = [[UIView alloc] initWithFrame:CGRectZero];
secondView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
secondView.backgroundColor = [UIColor blueColor];
}
return secondView;
}
// generate button on access
- (UIButton *)button {
if (button == nil) {
// create button
button = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
// set title
[button setTitle:#"Toggle Views"
forState:UIControlStateNormal];
// set self as a target for the "touch up inside" event of the button
[button addTarget:self
action:#selector(toggleViews)
forControlEvents:UIControlEventTouchUpInside];
}
return button;
}
// clean up
- (void)dealloc {
[button release];
[secondView release];
[firstView release];
[super dealloc];
}
#end
Use Interface Builder. It's there for a reason.