Setting up a subclass for UIButton to handle look-and-feel - iphone

I have a AuthButtons.h header:
#import <UIKit/UIKit.h>
#interface AuthButtons : UIButton
#property (nonatomic, weak) UIColor *backgroundColor;
#end
The implementation AuthButtons.m class is as follows:
#import "AuthButtons.h"
#implementation AuthButtons
- (void)setBackgroundColor:(UIColor *)backgroundColor
{
backgroundColor = [UIColor blueColor];
}
- (UIColor *)backgroundColor
{
return [UIColor whiteColor];
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
Then in my viewcontroller.m class I have:
#interface ViewController ()
#property (nonatomic, weak) AuthButtons *registerButton;
#end
...
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
_registerButton = [AuthButtons buttonWithType:UIButtonTypeSystem];
[_registerButton setTitle:#"Register" forState:UIControlStateNormal];
[_registerButton setBackgroundColor:[UIColor whiteColor]];
_registerButton.frame = CGRectMake(0.0, 0.0, self.screenWidth / 2.0, 100.0);
[_registerButton addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
}
It keeps crashing when I try to create the button. What am I missing?

Your button is weak and not added as a subview so its deallocated right away

Use strong instead of weak(i always use strong type when using buttons in ARC enabled class,not sure whether its a bad habbit :-))
#property (nonatomic, strong) AuthButtons *registerButton;
and add it as your subview([self.view addSubview:_registerButton]).Hope you have implemeted the function called -(void)buttonPressed:(id )sender in your class.

Related

initWithCoder doesn't set UILabel property (UIFont and text color)

I have the following code for my UICollectionViewCell
#property (strong, nonatomic) IBOutlet UIImageView *storyImage;
#property (strong, nonatomic) IBOutlet UILabel *titleLabel;
#property (strong, nonatomic) IBOutlet UILabel *descriptionLabel;
#end
#implementation Story
#synthesize storyImage;
#synthesize titleLabel;
#synthesize descriptionLabel;
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self)
{
[self.contentView addSubview:self.titleLabel];
[self.contentView addSubview:self.descriptionLabel];
[self.contentView addSubview:self.storyImage];
[self.titleLabel setFont:[UIFont fontWithName:kProximaNovaBold size:15]];
[self.descriptionLabel setFont:[UIFont fontWithName:kProximaNova size:13]];
[self.descriptionLabel setTextColor:[UIColor colorWithWhite:140/255.f alpha:1.0]];
self.descriptionLabel.frame = CGRectIntegral(self.descriptionLabel.frame);
self.descriptionLabel.center = roundedCenterPoint(self.descriptionLabel.center);
}
return self;
}
but for some reason it's not setting up the property. I'd have to move it out of the init method and put it inside a separate method and call this after calling:
dequeueReusableCellWithReuseIdentifier:kCellID forIndexPath:indexPath
and then it would work. Any idea why?
You want to be doing this stuff in awakeFromNib: when everything from the nib has actually been loaded and connected
From the docs for NSObject UIKit Additions
Discussion
The nib-loading infrastructure sends an awakeFromNib message to each object recreated from a nib archive, but only after all the objects in the archive have been loaded and initialized.

Customkeyboard Implemention with UISearchBar?

I try to implement customkeyboard with UISearchBar .first of all my mainclass.h where i use UISearchBar is look like this
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
#class PashtoKeyboard;
#class SearchBar;
#interface MainViewController : UIViewController<UISearchBarDelegate > {
IBOutlet SearchBar *searchBar;
PashtoKeyboard *pashtoKeyboard;
}
#property(nonatomic,retain) PashtoKeyboard *pashtoKeyboard;
#property (nonatomic,retain) SearchBar *searchBar;
#end
Now my mainclass.m
#import "MainViewController.h"
#import "PashtoKeyboard.h"
#import "SearchBar.h"
#implementation MainViewController
#synthesize pashtoKeyboard,searchBar;
- (void)viewDidLoad
{
[super viewDidLoad];
//Create Pashto Keyboard
PashtoKeyboard *pashtoKey = [[PashtoKeyboard alloc] initWithFrame:CGRectMake(0.0, 460.0-216.0, 320.0, 216.0)];
[pashtoKey loadView];
//[self.view addSubview:hebKey];
[pashtoKey addTarget:self action:#selector(pashtoKeyboard_clicked:) forControlEvents:1001];
[pashtoKey addTarget:self action:#selector(pashtoKeyboardBackspace_clicked:) forControlEvents:1002];
[pashtoKey addTarget:self action:#selector(pashtoKeyboardEnter_clicked:) forControlEvents:1003];
[self setPashtoKeyboard:pashtoKey];
//[hebKey setHidden:YES];
[pashtoKey release];
searchBar.inputView = pashtoKey;
searchBar.delegate=self;
searchBar.userInteractionEnabled=YES;
}
#pragma mark Pashto Keyboard Methods
- (void) pashtoKeyboard_clicked:(id)sender{
[searchBar setText:[pashtoKeyboard keyboardText]];
}
- (void) pashtoKeyboardBackspace_clicked:(id)sender{
[searchBar setText:[pashtoKeyboard keyboardText]];
}
- (void) pashtoKeyboardEnter_clicked:(id)sender{
[searchBar setText:[pashtoKeyboard keyboardText]];
}
In SearchBar.h
#import <UIKit/UIKit.h>
#interface SearchBar : UISearchBar {
}
#property (readwrite, retain) UIView *inputView;
#end
in SearchBar.m
#import "SearchBar.h"
#implementation SearchBar
#synthesize inputView;
- (void)dealloc {
[super dealloc];
}
#end
And then finally i have PashtoKeyboard.h and PashtoKeyboard.m where i create my custom keyboard,due to large coding i not show these classes code here i tested its with textveiw and textfield.
But it not works with UISearchBar .can some one guide me how to do this.thanx
You need to set the inputView of the UITextField that the UISearchBar is using in order to get the user input. You can do this by searching through the subviews of the search bar and finding the text field. Add this to the viewDidLoad method of your mainclass:
for (UIView *view in searchBar.subviews) {
if ([view isKindOfClass: [UITextField class]]) {
UITextField *textField = (UITextField *)view;
textField.inputView = // Your custom keyboard (PashtoKeyboard)
break;
}
}

Subclassed UIButton doesn't appear to initialize

This started as a simple problem, but I can't solve it. I have subclassed a UIButton (I know, controversial) but I can't seem to get it to work properly. My UIButton subclass looks like this:
.h
#interface SocialButton : UIButton
#property (nonatomic, strong) CustomClass *cust;
- (id)initWithObject:(CustomClass *)custom andFrame:(CGRect)frame;
#end
.m
#implementation SocialButton
#synthesize custom=_custom;
- (id)initWithObject:(CustomClass *)custom andFrame:(CGRect)frame;
{
self = [self initWithFrame:frame];
if (self)
{
[self addTarget:self action:#selector(buttonTouchUpInside:) forControlEvents:UIControlEventTouchUpInside];
self.backgroundColor = [UIColor blueColor];
self.cust = custom;
}
return self;
}
#end
Later, when I try to create a button from this class, like so:
SocialButton *social = [[SocialButton alloc] initWithObject:object andFrame:frame];
it doesn't appear to work. Any suggestions?
Seems like you forgot to call super. Change:
self = [self initWithFrame:frame];
to:
self = [super initWithFrame:frame];

Assigning a background color to a UITextField

I have a main controller header with a color declaration
MainViewController.h
#interface MainViewController : UIViewController <FlipsideViewControllerDelegate> {
UIColor *myColor;
}
#property (nonatomic, retain) UIColor *myColor;
#end
and an implementation with an initialization
MainViewController.m
#implementation MainViewController
#synthesize myColor;
- (void)viewDidLoad
{
[super viewDidLoad];
myColor = [UIColor colorWithRed:1.0 green:0.98 blue:0.8 alpha:1]; //initialization
}
Now I want to use this color in a method in MainViewController like this
- (void) myMethod {
myTextField.backgroundColor = myColor;
}
but but all my attempts are in vain. What is the correct syntax?
You must send a retain to the color object you initialize :
myColor = [[UIColor colorWithRed:1.0 green:0.98 blue:0.8 alpha:1] retain];
or set the property like so :
self.myColor = [UIColor colorWithRed:1.0
green:0.98
blue:0.8 alpha:1];
which, according to the property you declared, should retain it. The way you set the backgroundColor is ok.
if u not synthesize the myTextfield then
[myTextField setBackgroundColor:myColor];
if synthesize the myTextfield then
self.myTextField.backgroundColor = myColor;

Trying to get a UILabel to Show in a View, without a NIB

I'm new to iPhone programming, and I'm trying to make a simple program without a NIB. I have worked through some NIB tutorials, but I'd like to try some things programmatically.
My code loads without errors, makes the status bar black, and makes the background white. But, I don't think I'm loading my view with a label correctly after that. I presume I'm doing something just fundamentally wrong, so if you could point me in the right direction, I'd appreciate it. I think if I can get the label to show, I'll get some understanding. Here's my code:
//helloUAppDelegate.h
#import <UIKit/UIKit.h>
#import "LocalViewController.h"
#interface helloUAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
LocalViewController *localViewController;
}
#property (nonatomic, retain) UIWindow *window;
#property (nonatomic, retain) LocalViewController *localViewController;
#end
//helloUApDelegate.m
#import "helloUAppDelegate.h"
#implementation helloUAppDelegate
#synthesize window, localViewController;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
application.statusBarStyle = UIStatusBarStyleBlackOpaque;
window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
if (!window) {
[self release];
return;
}
window.backgroundColor = [UIColor whiteColor];
localViewController = [[LocalViewController alloc] init];
[window addSubview:localViewController.view];
// Override point for customization after application launch
[window makeKeyAndVisible];
}
//LocalViewController.h
#import <UIKit/UIKit.h>
#interface LocalViewController : UIViewController {
UILabel *myLabel;
}
#property (nonatomic, retain) UILabel *myLabel;
#end
//LocalViewController.m
#import "LocalViewController.h"
#implementation LocalViewController
#synthesize myLabel;
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
self.myLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 100, 200, 100)];
self.myLabel.text = #"Lorem...";
self.myLabel.textColor = [UIColor redColor];
}
- (void)dealloc {
[super dealloc];
[myLabel release];
}
Add your label to your LocalViewController's view:
- (void)loadView {
[super loadView];
self.myLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 100, 200, 100)];
self.myLabel.text = #"Lorem...";
self.myLabel.textColor = [UIColor redColor];
[self addSubview:self.myLabel];
[self.myLabel release]; // since it's retained after being added to the view
}