after try 2 days and i'm losing my mind now... the main problem is simple.. i've button in uiview,, and i want to call method on uiviewcontroller ... here's the code
Uiview call headerView.h
#class HeaderView;
#protocol headerViewDelegate
- (void)tes:(HeaderView *)tes ratingDidChange:(float)rating;
#end
#interface HeaderView : UIView {
UIButton *test;
}
#property (nonatomic,strong) UIButton *test;
#property (assign) id <headerViewDelegate> delegate;
#end
and the Headerview.M
#import "HeaderView.h"
#import <QuartzCore/QuartzCore.h>
#implementation HeaderView
#synthesize test;
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame]; // not needed - thanks ddickison
if (self) {
test=[UIButton buttonWithType:UIButtonTypeRoundedRect];
test.frame=CGRectMake(30, 10, 100,40);
[test setTitle:#"Kampret" forState:UIControlStateNormal];
[test addTarget:self action:#selector(testdata:) forControlEvents:UIControlEventTouchUpInside];
}
[self addSubview:test];
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder {
if ((self = [super initWithCoder:aDecoder])) {
}
return self;
}
- (void)dealloc {
[super dealloc];
}
-(void)layoutSubviews{
self.backgroundColor=[UIColor whiteColor];
[self release]; // release object before reassignment to avoid leak - thanks ddickison
//self = [nib objectAtIndex:0];
self.layer.shadowColor = [[UIColor blackColor] CGColor];
self.layer.shadowOffset = CGSizeMake(1.0, 1.0);
self.layer.shadowOpacity = 0.40;
}
-(void)testdata:(id)sender{
[self.delegate tes:self ratingDidChange:1.1f];
}
#end
viewcontroller.h
#import <UIKit/UIKit.h>
#import "HeaderView.h"
#interface ReusableTableHeadersViewController : UITableViewController <headerViewDelegate> {
HeaderView *header;
}
#property(nonatomic,strong) HeaderView *header;
#end
viewcontroller.m
#import "viewcontroller.h"
#implementation ReusableTableHeadersViewController
#synthesize header;
#synthesize tblData, data;
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
header = [[HeaderView alloc] initWithFrame:CGRectMake(0, 0, 320, 120)];
[self.view addSubview:header];
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
}
- (void)dealloc {
[super dealloc];
}
-(void)tes:(HeaderView *)tes ratingDidChange:(float)rating{
NSLog(#"passing data==========%f",rating);
}
#end
i want to log the rating :
NSLog(#"passing data==========%f",rating);
from method:
-(void)testdata:(id)sender{
[self.delegate tes:self ratingDidChange:1.1f];
}
but it show nothing... why? because i use initwithframe?
dont blame me... i'm totally newbie here :)
You have not set delegate of headerview as view controller.
- (void)viewDidLoad {
[super viewDidLoad];
header = [[HeaderView alloc] initWithFrame:CGRectMake(0, 0, 320, 120)];
header.delegate = self;
[self.view addSubview:header];
}
Related
I create a class named SWTextView and it inherits from UITextView. It does nothing but add a placeholder label. When I initialize a SWTextView and put it in a UITableViewCell, it's not editable even when the editable property of the SWTextView is YES. Does anyone have met the same problem?
The SWTextView is as follow:
#import <UIKit/UIKit.h>
#interface SWTextView : UITextView
- (void)setPlaceholder:(NSString *)placeholder;
#end
#interface SWTextView ()
#property(nonatomic, strong) UILabel *placeholderLabel;
#end
#implementation SWTextView
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self = [super initWithFrame:frame]) {
_placeholderLabel = [[UILabel alloc] init];
_placeholderLabel.textColor = [UIColor grayColor];
[self addSubview:_placeholderLabel];
self.font = [UIFont systemFontOfSize:12];
}
return self;
}
- (void)setFont:(UIFont *)font
{
[super setFont:font];
_placeholderLabel.font = font;
}
- (void)setPlaceholder:(NSString *)placeholder
{
_placeholderLabel.text = placeholder;
}
- (void)layoutSubviews
{
[super layoutSubviews];
[_placeholderLabel sizeToFit];
}
#end
I am trying to set up a class for a UI Button I created for a custom checkbox. For some reason my image is not being set by Xcode. Yet I have other classes in my application that are setting images without a problem.
Checkbox.h
#import <UIKit/UIKit.h>
#interface checkBoxButton : UIButton
#property (nonatomic,assign) IBInspectable BOOL checked;
#end
Checkbox.m
#import "checkBoxButton.h"
#implementation checkBoxButton
-(id) init {
self = [super init];
if(self) {
[self addTarget:self action:#selector(changeState) forControlEvents:UIControlEventTouchUpInside];
}
return self;
}
- (void)checkedBox:(BOOL) checked{
self.checked = checked;
if(!self.checked){
[self setImage: [UIImage imageNamed:#"checked.png"] forState:UIControlStateSelected];
} else {
[self setImage: [UIImage imageNamed:#"unchecked.png"] forState:UIControlStateNormal];
}
}
- (void) changeState {
self.checked = !self.checked;
[self sendActionsForControlEvents:UIControlEventValueChanged];
}
#end
-(id) init {
self = [super init];
if(self) {
self.checked = !self.checked;
[self addTarget:self action:#selector(checkedBox:) forControlEvents:UIControlEventTouchUpInside];
}
return self;
}
- (void)checkedBox:(BOOL) checked{
if(!self.checked){
self.checked = checked;
[self setImage: [UIImage imageNamed:#"checked.png"] forState:UIControlStateNormal];
} else {
self.checked = !checked;
[self setImage: [UIImage imageNamed:#"unchecked.png"] forState:UIControlStateNormal];
}
}
Try it.
UIButtonBox
//
// UIButtonBox.h
//
//
// Created by Nischal Hada on 9/16/15.
// Copyright (c) 2015 Nischal Hada. All rights reserved.
//
#import <UIKit/UIKit.h>
#interface UIButtonBox : UIButton
#property (nonatomic) BOOL isSelected;
#property (nonatomic,strong) NSString *selectImage;
#property (nonatomic,strong) NSString *deSelectImage;
- (void)select;
- (void)deselect;
- (void) setBackgroundImage:(UIImage *)image forState:(UIControlState)state;
#end
//
// UIButtonBox.m
//
//
// Created by Nischal Hada on 9/16/15.
// Copyright (c) 2015 Nischal Hada. All rights reserved.
//
#import "UIButtonBox.h"
#implementation UIButtonBox
- (void) layoutSubviews {
[super layoutSubviews];
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
- (id) initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
}
return self;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
- (void) setBackgroundImage:(UIImage *)image forState:(UIControlState)state {
[super setBackgroundImage:image forState:state];
}
- (void) setBackgroundImage:(NSString *)imageName {
self.isSelected = ([imageName isEqualToString:self.deSelectImage])?YES:NO;
[self setBackgroundImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
[self setBackgroundImage:[UIImage imageNamed:imageName] forState:UIControlStateSelected];
}
- (NSString *)selectImageName {
return self.selectImage;
}
- (NSString *)deSelectImageName {
return self.deSelectImage;
}
- (void)select {
[self setBackgroundImage:self.deSelectImage];
}
- (void)deselect {
[self setBackgroundImage:self.selectImage];
}
#end
CountrySelectCell
//
// CountrySelectCell.h
//
//
// Created by Nischal Hada on 12/12/15.
//
//
#import <UIKit/UIKit.h>
#import "UIButtonBox.h"
#interface CountrySelectCell : UITableViewCell
#property (strong, nonatomic) IBOutlet UILabel *lblName;
#property (nonatomic) BOOL isSelected;
#property (weak, nonatomic) IBOutlet UIButtonBox *btnCHeckBox;
- (IBAction)actionCheckBox:(id)sender;
#end
//
// CountrySelectCell.m
//
//
// Created by Nischal Hada on 12/12/15.
//
//
#import "CountrySelectCell.h"
#import "Constants.h"
#implementation CountrySelectCell
- (void)awakeFromNib {
self.lblName.font = [UIFont fontWithName:REGULAR_PROXIMANOVA_FONT size:SIZE_REGULAR__FONT];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
#pragma mark -
#pragma mark Object Methods
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
self.isSelected = NO;
}
return self;
}
#pragma mark -
#pragma mark IBAction Methods
- (IBAction)actionCheckBox:(id)sender {
self.isSelected = !self.isSelected;
(self.isSelected)?[self.btnCHeckBox select]:[self.btnCHeckBox deselect];
}
#end
CountrySelectVC.m
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"CountrySelectCell";
CountrySelectCell *cell = (CountrySelectCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"CountrySelectCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.lblName.text = [[tableData objectAtIndex:indexPath.row]uppercaseString];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.btnCHeckBox.deSelectImage = #"checkbox.png";
cell.btnCHeckBox.selectImage = #"uncheckbox.png";
[cell.btnCHeckBox setBackgroundImage:[UIImage imageNamed:#"uncheckbox.png"] forState:UIControlStateNormal];
return cell;
}
I am trying to call a UIView FirstScreen from my ViewController.
But I don't know how to do it.
ViewController:
#import "ViewController.h"
#import "FirstScreen.h"
#interface ViewController ()
{
FirstScreen *f;
}
FirstScreen.m:
#import "FirstScreen.h"
#implementation FirstScreen
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self didLoad];
}
return self;
}
-(void) didLoad{
[self addLabel];
[self addImageView];
}
#implementation FirstScreen
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self didLoad];
}
return self;
}
-(void) didLoad
{
f = [[FirstScreen alloc] initWithFrame:CGRectMake(0, 0, self.view.size.width, self.view.size.height)];
[self.view addSubview:f];
}
#import "FirstScreen.h"
#implementation FirstScreen
-(instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
// calling another methods
[self anotherMethod];
return self;
}
-(void) anotherMethod
{
// write some code here
}
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
FirstScreenObj = [[FirstScreen alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
FirstScreenObj.backgroundColor = [UIColor grayColor];
[self.view addSubview:FirstScreenObj];
}
I hope it will help you!
I'm trying to structure some view-code out of the controller (obviously without using a nib). Therefore, I tried a simple example, but for some reason, I can't add the target to the button in the controller, rest is fine. Here's what I'm trying:
Controller.h:
#import <UIKit/UIKit.h>
#import "IndexView.h"
#interface IndexController : UIViewController
{
}
#property (nonatomic) IndexView *contentView;
#end
Controller.m
#import "IndexController.h"
#import "IndexView.h"
#interface IndexController ()
#end
#implementation IndexController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.contentView = [[IndexView alloc]init];
[self.view addSubview:self.contentView];
[self connectUIElements];
}
return self;
}
- (void) connectUIElements
{
[self.contentView.testButton addTarget:self action:#selector(testButtonClicked) forControlEvents:UIControlEventTouchUpInside];
}
#pragma mark --
#pragma mark UIHandler
- (void) testButtonClicked
{
NSLog(#"testbutton clicked");
}
#end
View.h
#import <UIKit/UIKit.h>
#interface IndexView : UIView
#property (nonatomic, strong) UIButton *testButton;
#end
View.m
#import "IndexView.h"
#implementation IndexView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setUpView];
}
return self;
}
- (id) init
{
self = [super init];
if (self) {
[self setUpView];
}
return self;
}
- (void) setUpView
{
[self setBackgroundColor:[UIColor whiteColor]];
self.testButton = [[UIButton alloc]initWithFrame:CGRectMake(10, 50, 100, 50)];
self.testButton.backgroundColor = [UIColor greenColor];
[self.testButton setTitle:#"hello, world" forState:UIControlStateNormal];
[self addSubview:self.testButton];
}
#end
I'm just exploring possibilities to get closer to a classical MVC pattern and a little farther away from Apples mediator-interpretation. Any idea what's wrong?
You have to create your view using initWithFrame:, which is the default initializer for UIView. (The reason that is does not work with a simple init - but I am guessing here! - might be that the views size is zero and therefore the touch events do not work.)
I have a tabled application built with storyboards, so I would like to add a paging possibility for the items in table. I used this tutorial:
So I added the new view to the storyboard and the UIScrollView on the view. Than made a property UIScrollView* scrollView; and make the relation scrollView - ECOMClPanelPagingViewController in storyboard. (ECOMClPanelPagingViewController is a UIViewController class for my view.
.h
#import <UIKit/UIKit.h>
#interface ECOMClPanelPagingViewController : UIViewController
{
UIScrollView *scrollView;
}
#property (nonatomic, retain) IBOutlet UIScrollView* scrollView;
#end
.m
#import "ECOMClPanelPagingViewController.h"
#interface ECOMClPanelPagingViewController ()
#end
#implementation ECOMClPanelPagingViewController
#synthesize scrollView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray *colors = [NSArray arrayWithObjects:[UIColor redColor], [UIColor greenColor], [UIColor blueColor], nil];
for (int i = 0; i < colors.count; i++) {
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
UIView *subview = [[UIView alloc] initWithFrame:frame];
subview.backgroundColor = [colors objectAtIndex:i];
[self.scrollView addSubview:subview];
[subview release];
}
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * colors.count, self.scrollView.frame.size.height);
}
- (void)viewDidUnload
{
self.scrollView = nil;
}
- (void)dealloc
{
[scrollView release];
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#end
So I see only the white background with navigation controller.. Who can help me with it?
Just need to uncheck the Use Autolayout option