How do I use a subclass instead of intended class? - iphone

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)];

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];

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.

Create a custom UIView as a tableHeaderView programmatically

I want to create a 'detail view' in my navigation-based app similar to the address book app.
I want to create a UIView that has an UIImageView and a UILabel that I can pass to a UITableVIew's tableHeaderView property when pushed by a navigation controller. The label text and image must take information from the managed object context when it loads.
I started trying to do this using IB, but go fed up when the label text wouldn't update, regardless of where I put the myView.UILabel.text = #"some new text". It just kept presenting the text I entered in the IB inspector window.
So without using IB, how would I go about creating a UIView that has a UILabel and a UIImageView in it?
Should I be creating a new class that is a sub-class of UIViewController or just UIView?
How do I go about creating the sub-views for the label and image?
Where should the text and image URL be set in code? (viewDidLoad or loadView or viewWillLoad)(UIView, UIViewController, detailViewController)?
If you started using IB and the UILabel text wouldn't update, sounds like the IBOutlet of your view controller isn't correctly connected to the label in IB.
Hopefully that will fix your problem with using IB. And in the viewController code, you should update that text in viewDidLoad.
But if you want to do this programmatically, I would make a subclass of UIView (not UIViewController).
And I would override initWithFrame and do all the setup there.
Something like the following:
myView.m
#implementation myView
#synthesize imageView, myLabel;
- (id) initWithFrame:(CGRect) frame
{
if (self = [super initWithFrame:frame]) {
// Setup your image view and add it to the view.
self.imageView = [[[UIImageView alloc] initWithFrame:initWithFrame:frame] autorelease];
self.imageView.image = ...
[self addSubview:self.imageView];
// Setup your label
self.myLabel = [[[UILabel alloc] initWithFrame:frame] autorelease];
self.myLabel.text = #"whatever you like";
[self addSubview:self.myLabel];
}
return self;
}
Make sure to clean up memory properly in the dealloc method of course depending on whether you like make class properties vs class variables.

How to add action to one dynamic button in one UIView Control to move back to UIViewController

In my application, i am using UIViewController and dynamically created UIView.In that view dynamically created subviews(UIButton and PickerViewController)..I want to display selected picker view value in a label control in UIViewController... How to add action to this button to move back to UIViewController...
You can either pass the reference of the UIViewCOntroller to the subview or you can create delegate methods for the subview.
Method 1 :
//below code is written in your UIViewController
/*You should write the implementation of initWithFrame:viewController: in UIVIEW
CustomUIView *youSubView = [[CustomUIView alloc]initWithFrame : yourFrame viewController:self];
//below code is in CustomUIView
- (id) initWithFrame :(CGRect)frame viewController:(id)vc
{
....
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn addTarget:vc action:methodinVc forControlEvents:UIControlEventTouchUpInside];
}
Method 2 :
Make protocol and delegate in UIView and make the UIViewController to respond to the delegate calls

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

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.