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
Related
I have made a UITableViewCell with xib, inside it I have made a UIView and made IBoutlet in my custom tableViewCell class. I want to set the border color of that UIView.
My code in tableViewCell.h:
#property (weak, nonatomic) IBOutlet UIView *circleView;
In tableViewCell.m:
#import "OUSTProfileTableViewCell.h"
#implementation OUSTProfileTableViewCell
//#synthesize circleView = _circleView;
- (void)awakeFromNib {
[super awakeFromNib];
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
}
return self;
}
- (instancetype)initWithCoder:(NSCoder *)coder
{
self = [super initWithCoder:coder];
if (self) {
self.circleView.layer.cornerRadius = 3; // this value vary as per your desire
self.circleView.layer.masksToBounds = YES;
self.circleView.layer.borderWidth = 2.0;
self.circleView.layer.borderColor = (__bridge CGColorRef _Nullable)([UIColor lightGrayColor]);
}
return self;
}
#end
But it's not working.
Put code inside
- (void)awakeFromNib {
[super awakeFromNib];
// Initialization code
self.circleView.layer.cornerRadius = 3; // this value vary as per your desire
self.circleView.layer.masksToBounds = YES;
self.circleView.layer.borderWidth = 2.0;
self.circleView.layer.borderColor = [UIColor lightGrayColor].CGColor;
}
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 subclassed UIControl to create my own class. I am trying to override setHighligheted: but for some reason it doesn't get called...
I have double checked and none of my subviews have userInteractionEnabled = NO. Are there any other methods that I should override?.
Thank you!
Here is some sample code:
#import "Booking.h"
#interface BookingCloud : UIControl
// Booking
#property(nonatomic, strong) Booking *booking;
// BackgroundView
#property (strong, nonatomic) IBOutlet UIView *backgroundView;
And the implementation
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
[self setUpSubViews];
}
return self;
}
- (void)setUpSubViews {
[[NSBundle mainBundle] loadNibNamed:#"BookingCloud" owner:self options:nil];
[self addSubview:backgroundView];
self.frame = backgroundView.frame;
self.layer.cornerRadius = 10;
self.layer.masksToBounds = YES;
}
- (void)setHighlighted:(BOOL)highlighted {
[super setHighlighted:highlighted];
if (highlighted) {
backgroundView.backgroundColor = [[Utils colorWithHexString:[[ConfigurationManager instance] UIConfigValueForKey:#"background_color"]] colorWithAlphaComponent:0.8];
} else {
backgroundView.backgroundColor = [[Utils colorWithHexString:[[ConfigurationManager instance] UIConfigValueForKey:#"background_color"]] colorWithAlphaComponent:1];
}
}
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
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];
}