I create a tableView in a class inherit from UIView.It works well when I add a button at tableFooterView on this tableView.
CustomView.h
#import <UIKit/UIKit.h>
#interface CustomView : UIView<UITableViewDelegate,UITableViewDataSource>
#end
CustomView.m
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
UIView * customView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, 80)];
UIButton * tempButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 40)];
tempButton.center = customView.center;
tempButton.backgroundColor = [UIColor redColor];
[tempButton addTarget:self action:#selector(doButtonAction) forControlEvents:UIControlEventTouchUpInside];
[customView addSubview:tempButton];
return customView;
}
- (void)doButtonAction{
NSLog(#"123");
}
NSLog:123
However,I public this button and make a target-action in my controller.It response nothing.
CustomView.h
#property (strong, nonatomic) UIButton * tempButton;
CustomView.m
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
UIView * customView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, 80)];
self.tempButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 40)];
self.tempButton.center = customView.center;
[customView addSubview:self.tempButton];
return customView;
}
ViewController.h
- (void)viewDidLoad {
[super viewDidLoad];
CustomView *cv = [[CustomView alloc] initWithFrame:self.view.bounds ];
[cv.tempButton addTarget:self action:#selector(doButtonAction) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:cv];
}
- (void)doButtonAction{
NSLog(#"123");
}
what did I miss?
In the viewdidload you are not set any thing on the button,
You just assign in the #interface.
but you must be alloc and set the frame and backgroundcolor and all properties on the button in the viewdidload .
After that it will work.
May be it will help you or feel free.
Related
(I'm currently learning iOS Development so I apologize in advanced of my limited knowledge.)
What I am trying to accomplish is that whenever you press either the addition or subtraction button the label will increment by one and will display the result by the label. I am having difficulty with displaying the result in the label.
This is my ViewController.h file:
#import <UIKit/UIKit.h>
#import "StepperView.h"
#interface ViewController : UIViewController{
UILabel *valueLabel;
}
#end
This is my ViewController.m file:
#import "ViewController.h"
#import "StepperView.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
int w;
w = 0;
[super viewDidLoad];
StepperView *stepperView = [[StepperView alloc] initWithFrame:CGRectMake(0, 0, 300, 50)];
stepperView.center = self.view.center;
[self.view addSubview:stepperView];
// AddButton
UIButton *additionButton = [[UIButton alloc] initWithFrame:CGRectMake(195, 318, 100, 100)];
additionButton.frame = CGRectMake(stepperView.frame.size.width/2 + 50, stepperView.frame.size.height/2 - 25, 100, 50);
[self.view addSubview:additionButton];
// Subtraction button
UIButton *subtractionButton = [[UIButton alloc] initWithFrame:CGRectMake(195, 318, 100, 100)];
subtractionButton.frame = CGRectMake(stepperView.frame.size.width/2 - 150, stepperView.frame.size.height/2 - 25, 100, 50);
[self.view addSubview:subtractionButton];
// Label
valueLabel = [[UILabel alloc] initWithFrame:CGRectMake(195, 318, 100, 100)];
[valueLabel setTextColor:[UIColor redColor]];
[self.view addSubview:valueLabel];
}
#end
This is my Delegate and Protocol:
StepperView.h
#import <UIKit/UIKit.h>
#protocol SwitchViewDelegate
- (void)switchViewValueChanged:(int)number;
#end
#interface StepperView : UIView
{
}
#property (nonatomic,weak) id<SwitchViewDelegate> delegate;
#end
This is my StepperView.m file
#import "StepperView.h"
#implementation StepperView
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
[self setup];
return self;
}
- (void)setup {
UIView *stepperView = [[UIView alloc] init];
stepperView.frame = CGRectMake(0, 0, 300, 40);
stepperView.backgroundColor = [UIColor colorWithRed:21/255.0 green:101/255.0 blue:192/255.0 alpha:1.0];
[self addSubview:stepperView];
// Add Button
UIButton *rightView = [[UIButton alloc] init];
rightView.frame = CGRectMake(stepperView.frame.size.width/2 + 50, stepperView.frame.size.height/2 - 20, 100, 40);
rightView.backgroundColor = [UIColor colorWithRed:33/255.0 green:150/255.0 blue:243/255.0 alpha:1.0];
[rightView setTitle:#"+" forState:UIControlStateNormal];
[rightView addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:rightView];
// Subtract Button
UIButton *leftView = [[UIButton alloc] init];
leftView.frame = CGRectMake(stepperView.frame.size.width/2 - 150, stepperView.frame.size.height/2 - 20, 100, 40);
leftView.backgroundColor = [UIColor colorWithRed:33/255.0 green:150/255.0 blue:243/255.0 alpha:1.0];
[leftView setTitle:#"-" forState:UIControlStateNormal];
[leftView addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:leftView];
// Label
UILabel *valueLabel = [[UILabel alloc] initWithFrame:CGRectMake(stepperView.frame.size.width/2, stepperView.frame.size.height/2, 100, 100)];
[valueLabel setText:#"10"];
[valueLabel setTextColor:[UIColor redColor]];
[self addSubview:valueLabel];
}
- (void)buttonPressed:(id)sender{
UIButton *button = (UIButton *) sender;
NSString *title = button.titleLabel.text;
if([title isEqualToString:#"-"]) {
NSLog(#"MINUS");
} else {
NSLog(#"PLUS");
}
// NSLog(#"%#",title);
}
#end
I am not sure if i understand clear what you want to do, but why don't you try to use the UIStepper button provided by iOS?
You can add an IBAction to your viewController and change the label with the value from the UIStepper.
Using swift:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var valueLabel: UILabel!
var currentValue = 0
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func touchStepper(sender: UIStepper) {
self.valueLabel.text = "\(sender.value)"
}
}
It's not very clear what you're trying you obtain as UI, i'm posting a possible solution which is using
The delegate to pass the information from StepperView to the ViewController
UILabel to present the result which is subview of ViewController's view, instead to be subview of StepperView
ViewController.h:
#import <UIKit/UIKit.h>
#import "StepperView.h"
#interface ViewController : UIViewController
#property (nonatomic, strong) UILabel *valueLabel;
#end
ViewController.m
#import "ViewController.h"
#import "StepperView.h"
#interface ViewController () <StepperViewDelegate>
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
StepperView *stepperView = [[StepperView alloc] initWithFrame:CGRectMake(0, 0, 300, 50)];
stepperView.center = self.view.center;
[self.view addSubview:stepperView];
stepperView.delegate = self;
// Label
self.valueLabel = [[UILabel alloc] initWithFrame:CGRectMake(195, 318, 100, 100)];
[self.valueLabel setTextColor:[UIColor redColor]];
[self.view addSubview:self.
valueLabel];
}
#pragma mark - StepperViewDelegate
- (void)stepperView:(StepperView *)stepperView valueChanged:(NSInteger)value {
self.valueLabel.text = [NSString stringWithFormat:#"%d", value];
}
#end
StepperView.h
#import <UIKit/UIKit.h>
#class StepperView
#protocol StepperViewDelegate
- (void)stepperView:(StepperView *)stepperView valueChanged:(NSInteger)value
#end
#interface StepperView : UIView
#property (nonatomic, weak) id<StepperViewDelegate> delegate;
#property (nonatomic, assign) NSInteger stepperValue;
#end
StepperView.m
#import "StepperView.h"
#implementation StepperView
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
[self setup];
return self;
}
- (void)setup {
UIView *stepperView = [[UIView alloc] init];
stepperView.frame = CGRectMake(0, 0, 300, 40);
stepperView.backgroundColor = [UIColor colorWithRed:21/255.0 green:101/255.0 blue:192/255.0 alpha:1.0];
[self addSubview:stepperView];
// Add Button
UIButton *rightView = [[UIButton alloc] init];
rightView.frame = CGRectMake(stepperView.frame.size.width/2 + 50, stepperView.frame.size.height/2 - 20, 100, 40);
rightView.backgroundColor = [UIColor colorWithRed:33/255.0 green:150/255.0 blue:243/255.0 alpha:1.0];
[rightView setTitle:#"+" forState:UIControlStateNormal];
[rightView addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:rightView];
// Subtract Button
UIButton *leftView = [[UIButton alloc] init];
leftView.frame = CGRectMake(stepperView.frame.size.width/2 - 150, stepperView.frame.size.height/2 - 20, 100, 40);
leftView.backgroundColor = [UIColor colorWithRed:33/255.0 green:150/255.0 blue:243/255.0 alpha:1.0];
[leftView setTitle:#"-" forState:UIControlStateNormal];
[leftView addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)buttonPressed:(id)sender{
UIButton *button = (UIButton *) sender;
NSString *title = button.titleLabel.text;
if([title isEqualToString:#"-"]) {
self.stepperValue--;
} else {
self.stepperValue++;
}
[self.delegate stepperView:self valueChanged:self.stepperValue];
// NSLog(#"%#",title);
}
#end
This should give you more clear vision how different components should corespondent with each other.
I added a header view with a button & a imageview but I can not Identify the Imageview for changed the Image.
My code is as follow:
My HaderView class .h file (HeaderSection.h)
#import <UIKit/UIKit.h>
#interface HeaderViewSection : UIView
#property(nonatomic,retain)UIButton *btn;
#property(nonatomic,retain)UIImageView *arrow_img;
#end
My HaderView class .m file (HeaderSection.m)
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.backgroundColor = [UIColor brownColor];
_arrow_img=[[UIImageView alloc]init];
_arrow_img.frame=CGRectMake(280, 15, 20, 20);
_arrow_img.image=[UIImage imageNamed:#"Arrow_down_section.png"];
_arrow_img.backgroundColor=[UIColor clearColor];
[self addSubview:_arrow_img];
btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(20, 15, 35, 25);
[btn setTitle:#"R" forState:UIControlStateNormal];
btn.backgroundColor = [UIColor blackColor];
[self addSubview:btn];
}
return self;
}
.h file
#import "HeaderViewSection.h"
#property (nonatomic,retain)HeaderViewSection *header_view;
.m file
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
_header_view = [[HeaderViewSection alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
_header_view.tag = section+5000;
_header_view.btn.tag = section+5000;
[_header_view.btn addTarget:self action:#selector(expandTableHeaderView:) forControlEvents:UIControlEventTouchUpInside];
_header_view.arrow_img.tag=section+2000;
return _header_view;
}
-(void)expandTableHeaderView:(id)sender{
[self.tblView beginUpdates];
NSInteger section = [sender tag];
UIView *view = (UIView *)[_header_view viewWithTag:[sender tag]]; //RETURNS nil
NSArray *arr = [view subviews]; //RETURNS nil
}
I don't know why this happen? Please Help me to solve this.
In this method you are creating new headerView. Now at this time there is no subView in headerView.
You are setting headerView.btn.tag but you did not add button in headerView, same is the case with image.
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
_header_view = [[HeaderViewSection alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
_header_view.tag = section+5000;
UIButton *button = [UIButton alloc] init];//set frame also
button.tag = section+5000;
[button addTarget:self action:#selector(expandTableHeaderView:) forControlEvents:UIControlEventTouchUpInside];
[header_view addSubView:button];
//Do same like image view
}
Edied:
As -(void)expandTableHeaderView:(id)sender{ } method will call only when you tap on button. Button is a subView of headerView. You can have header view by calling its superView method like this
UIView *view = (UIView*)[sender superView];
view.frame = // new frame
now view is you headerView, you can change its frame to expand or what ever you want.
You can get its subViews like this
NSArray *array = [view subViews];
for(UIView *v in array){
if([v isKindOfClass:[UIImage class]]){
//v is imageview change image if you want
}
I'm try to add a scrollview totally by code to a modal view, but it doesn't show.
I've tried different ways but I can't see the mistake,
Somebody could help me?
here my code:
#interface AddWithScrollOrizontal ()<UIScrollViewDelegate>{
//IBOutlet UIScrollView*scroll;
}
#property (strong, nonatomic) UIScrollView*scroll;
#implementation AddWithScrollOrizontal
#synthesize scroll;
- (void)viewDidLoad
{
self.scroll = [[UIScrollView alloc]init];
self.scroll.delegate = self;
if (IS_IPHONE_5) {
self.scroll.frame = CGRectMake(0, 58, 320, 270);
}else{
self.scroll.frame = CGRectMake(0, 20, 320, 270);
}
self.scroll.backgroundColor = [UIColor redColor];
self.scroll.pagingEnabled = YES;
self.scroll.contentSize =CGSizeMake(1280, 270);
UIView*firstView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 270)];
...
UIView*fourthView = [[UIView alloc]initWithFrame:CGRectMake(960, 0, 320, 270)];
[self.scroll addSubview:firstView];
[self.scroll addSubview:secondView];
[self.scroll addSubview:thirdView];
[self.scroll addSubview:fourthView];
[self.view addSubview:self.scroll];
}
You say you're adding it totally by code, but you're making an IBOutlet to scroll. Did you create it in IB? If not, your problem is that you never instantiate the scroll view. Also, there's no need to create an ivar for scroll, just create the property inside the #interface declaration for your class extension, and refer to it with self.scroll.
I wanted to use the bounds of a UIView to create a UILabel and add it to that UIView inside the viewDidLoad method, however, I have found that the bounds of UIView is still null inside viewDidLoad. But when I look at a demo app, the bounds of its UIView is already initialised in viewDidLoad.
In the interface I have
#interface ViewController : UIViewController
#property (weak, nonatomic) IBOutlet UIView *promotionView;
#end
And I am trying to do
- (void)viewDidLoad
{
[super viewDidLoad];
UIView *container = [[UIView alloc] initWithFrame:self.promotionView.bounds];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, container.bounds.size.height-100, 320, 20)];
label.text = #"Promotion Title";
[container addSubview:label];
[self.promotionView addSubview: container];
}
but it doesn't work because the bounds of promotionView is null.
You can do it like this in viewDidAppear:
-(void)viewDidAppear:(BOOL)animated {
static int first = 1;
if (first) {
UIView *container = [[UIView alloc] initWithFrame:self.promotionView.bounds];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, container.bounds.size.height-100, 320, 20)];
label.text = #"Promotion Title";
[container addSubview:label];
[self.promotionView addSubview: container];
first = 0;
}
}
I would like to create a button with an image, title and description similar to the UITableViewCellStyleSubtitle. I would like to do this programmatically.
Does anyone know how I can accomplish this?
You could create a category of the UIButton class. Here's a barebones setup for you:
In the .h file:
#interface UIButton (YourCategoryName)
- (void)setupButton:(NSString *)title image:(UIImage *)image description:(NSString *) description;
#end
In the .m file:
#implementation UIButton (YourCategoryName)
- void)setupButton:(NSString *)title image:(UIImage *)image description:(NSString *) description {
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 0, 200, 50)]];
[self addSubview:titleLabel];
[titleLabel release];
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:image];
[self addSubview:imageView];
[imageView release];
UILabel *descriptionLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 60, 200, 100)];
[self addSubview:descriptionLabel];
[descriptionLabel release];
}
In the above code, you can adjust the frames as needed. Once this has been setup, just create your button like normal in your view/viewcontroller and call the method above:
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
myButton.frame = CGRectMake(0, 0, 300, 200);
[myButton setupButton:myTitle image:myImage description:myDesc];
Subclass UIControl and override drawRect and/or add subviews to create the desired appearance.