Add a button on the custom UITableViewHeaderFooterView - ios

I am trying to add a button programmatically on the custom UITableViewHeaderFooterView class as follows, it does not appear.
I wonder what I am missing or doing wrong ?
- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier
{
if (self = [super initWithReuseIdentifier:reuseIdentifier]) {
UIButton * clickBtn = [[UIButton alloc]initWithFrame:CGRectMake(self.contentView.bounds.size.width-50, 0, 50, 50)];
clickBtn.imageView.image = [UIImage imageNamed:#"carat-open.png"];
[self.contentView addSubview:clickBtn];
}
return self;
}

Create UIButton inside of this function and add button as a subview to header view
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIButton * clickBtn = [[UIButton alloc]initWithFrame:CGRectMake(self.contentView.bounds.size.width-50, 0, 50, 50)];
clickBtn.imageView.image = [UIImage imageNamed:#"carat-open.png"];
UIView *headerView = [[UIView alloc] init];
[headerView addSubview:clickBtn];
headerView.backgroundColor=[UIColor colorWithRed:0.95 green:0.95 blue:0.95 alpha:1.0];
return headerView;
}

To add custom view in Section Footer:
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 20;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
return [self getViewForFooterForSection];
}
- (UIView*)getViewForFooterForSection
{
UIView *sectionFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 20)];
sectionFooterView.backgroundColor = [UIColor redColor];
return sectionFooterView;
}
To add custom view for section footer from custom view file
CustomFooter
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if(self)
{
self.frame = frame;
self.backgroundColor = [UIColor blueColor];
}
return self;
}
View Controller
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
return [self getViewForFooterForSection];
}
- (UIView*)getViewForFooterForSection
{
CustomFooter *sectionFooterView = [[CustomFooter alloc] initWithFrame:CGRectMake(0, 0, 100, 20)];
return sectionFooterView;
}
To add custom view in Table footer View
tbl.tableFooterView = [self getViewForTableView];
- (UIView*)getViewForTableView
{
UIView *tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 20)];
tableFooterView.backgroundColor = [UIColor greenColor];
return tableFooterView;
}

you can't set a button image like that .
clickBtn.imageView.image = [UIImage imageNamed:#"carat-open.png"];
imageView property is readonly.
use this one
(void)setImage:(nullable UIImage *)image forState:(UIControlState)state;

Related

Custom View add SubView layout issue

There is a tableView with 2 sections, which have a customized section header view for each.
I want UI of this effect, and header view of section 0 works fine.
While header view of section 1 has an issue.
I don't know what's wrong.
What Apple mechanism do I miss,
convert rect/point?
Here is the code:
the customized view of section 1
#interface ZBWithDrawHeaderView()
#property (nonatomic, strong ) UIView *renderView;
#end
#implementation ZBWithDrawHeaderView
- (instancetype)initWithFrame:(CGRect)frame{
if (self = [super initWithFrame:frame]) {
self.backgroundColor = [UIColor grayColor ];
_renderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, -17)];
//it works OK, I don't know the logic.
// _renderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 17)];
//it works Wrong ,what mechanism do I miss
[self addSubview: _renderView];
_renderView.backgroundColor = [UIColor redColor ];
}
return self;
}
#end
Where I put it into use?
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
if(section == 0){
......
return view;
}
else if (section == 1) {
ZBWithDrawHeaderView * secondHeaderView = [[ZBWithDrawHeaderView alloc] initWithFrame: CGRectMake(0, 0, KScreenWidth, 25)];
return secondHeaderView;
}
return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
return CGFLOAT_MIN;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
return [UIView new ];
}
The two UITableViewDelegate methods added, no strange layout happens. Seems like section header and footer must come in pair.

How to indent only UITableViewCell content not the separator?

If I use this code to indent from left, everything is indented (separator + content)
table.contentInset = UIEdgeInsetsMake(0, 20, 0, 0);
Same if I use this:
cell.layoutMargins = UIEdgeInsetsMake(0, 20, 0, 0);
This doesn't help neither, because it doesn't move the image.
cell.indentationLevel = 10;
You can use a CustomTableCell to get what you want:
At first, in ViewController:
#import "ViewController.h"
#import "MyTableCell.h"
#interface ViewController ()
#property UITableView *tableView;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
_tableView.separatorStyle = UITableViewCellSelectionStyleNone;
_tableView.backgroundColor = [UIColor grayColor];
_tableView.dataSource = self;
_tableView.delegate = self;
[self.view addSubview:_tableView];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 20;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
MyTableCell *cell = [tableView dequeueReusableCellWithIdentifier:#"MyCell"];
if (nil == cell) {
cell = [[MyTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"MyCell"];
}
//Update data for cell
return cell;
}
#end
This is MyTableCell.h:
#import "MyTableCell.h"
#interface MyTableCell()
#property UIView *containerView;
#property UIView *separationLine;
#end
#implementation MyTableCell
-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
_containerView = [[UIView alloc] init];
_containerView.backgroundColor = [UIColor redColor];
[self addSubview:_containerView];
_separationLine = [[UIView alloc] init];
_separationLine.backgroundColor = [UIColor blackColor];
[self addSubview:_separationLine];
return self;
}
-(void)layoutSubviews{
_containerView.frame = CGRectMake(20, 0, self.frame.size.width-20, self.frame.size.height-1);
_separationLine.frame = CGRectMake(10, self.frame.size.height-1, self.frame.size.width-10, 1);
}
#end
And this is the screenshot for the code:
You can modify the code in "layoutSubviews" as your wish.
Hope it can help you.

How to add UIImageView on popup block in ios

Hi I am very new to iOS and in my project iI have created one UITableView with images as like my below screen ok that's fine
And here when I tapped on tableView images I want show that related row images with popup block using one UIView.
But using my code image is not showing on popup UIView. Please see my below screen image is not adding on UIView popup block please help me some one.
My Code:
#import "imageTableViewController.h"
#interface imageTableViewController ()<UITableViewDataSource,UITableViewDelegate>
{
UITableView *mainTable;
NSMutableArray *imageArray;
UIButton *imageButton;
UIView *popUpView;
BOOL isFullScreen;
CGRect prevFrame;
}
#end
#implementation imageTableViewController
- (void)viewDidLoad {
[super viewDidLoad];
imageArray = [[NSMutableArray alloc] initWithObjects:#"flower.jpeg",#"Bird.jpg",#"Browser.jpeg", nil];
mainTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, 320, 480)];
mainTable.dataSource = self;
mainTable.delegate = self;
[self.view addSubview:mainTable];
popUpView = [[UIView alloc] initWithFrame:CGRectMake(30, 30, 25, 25)];
popUpView.backgroundColor = [UIColor orangeColor];
popUpView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.001, 0.001);
[mainTable addSubview:popUpView];
UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 20, 20)];
imageView.image = [UIImage imageNamed:#"flower.jpeg"];
[popUpView addSubview:imageView];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return imageArray.count;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"cell";
UITableViewCell *Cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (Cell == nil)
{
Cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
imageButton = [[UIButton alloc] initWithFrame:CGRectMake(10, 10, 50, 50)];
[imageButton setImage:[UIImage imageNamed:[imageArray objectAtIndex:indexPath.row]] forState:UIControlStateNormal];
[imageButton addTarget:self action:#selector(imgToFullScreen:) forControlEvents:UIControlEventTouchUpInside];
[Cell.contentView addSubview:imageButton];
return Cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 60;
}
-(void)imgToFullScreen:(UITapGestureRecognizer*)sender {
if (!isFullScreen) {
[UIView animateWithDuration:0.5 delay:0 options:0 animations:^{
popUpView.hidden = NO;
prevFrame = CGRectMake(30, 30, 25, 25);
[popUpView setFrame:CGRectMake(120, 50, 150, 150)];
}completion:^(BOOL finished){
isFullScreen = TRUE;
}];
return;
}
else{
[UIView animateWithDuration:0.5 delay:0 options:0 animations:^{
[popUpView setFrame:prevFrame];
}completion:^(BOOL finished){
isFullScreen = FALSE;
popUpView.hidden = YES;
}];
return;
}
}
Looks like it's happened because of
popUpView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.001, 0.001);
you create view and scale it to really small, also all subview will be downscaled. And then in show animation block you change frame of popup, but you have to change scale like:
popUpView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1, 1);
Edit due comment
hmm yes that's fine and one small problem is coming #in.disee that is
when i zoom in popup block images are popup like my above screen and
when i zoom out that images are must zoom out near related
rows,understand?
Your current realisation have few problems including:
Your architecture do not allow you get index path of selected cell - it will be a problem anyway, so among other you have to make this part.
It will be too hard explain in words everything you have to chage, so i write code for you) I almost do not change your code, but it would be cool if you change it to the way i write mine, because it's more convenient to apple guides
What i do:
1) create custom class for cell
2) add ability for cell say something to tableview via delegation pattern
3) when user click on button in cell - cell tells to tableview, which button of which was was pressed
4) table view convert frame of cell's image to own coordinates and show popup
you can just replace you code with this one:
#import "imageTableViewController.h"
#class CustomCell;
#protocol CustomCellDelegate <NSObject>
- (void)didSelectImageNamed:(NSString *)name fromCell:(CustomCell *)cell;
#end
#interface CustomCell : UITableViewCell
#property (nonatomic, strong) NSString *imageName;
#property (nonatomic, strong) UIButton *imageBtn;
#property (nonatomic, weak) id <CustomCellDelegate> delegate;
#property (nonatomic, assign, readonly) CGRect imageRect;
#end
#implementation CustomCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self){
[self setup];
}
return self;
}
- (CGRect)imageRect {
return self.imageBtn.frame;
}
- (void)setup {
self.imageBtn = [[UIButton alloc] initWithFrame:CGRectMake(10, 10, 50, 50)];
[self.imageBtn addTarget:self action:#selector(imgToFullScreen:) forControlEvents:UIControlEventTouchUpInside];
[self.contentView addSubview:self.imageBtn];
}
- (void)prepareForReuse {
[self.imageBtn setImage:nil forState:UIControlStateNormal];
}
- (void)setImageName:(NSString *)imageName {
_imageName = imageName;
[self.imageBtn setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
}
- (void)imgToFullScreen:(UIButton *)sender {//btw it's not UITapGestureRecognizer *
[self.delegate didSelectImageNamed:self.imageName fromCell:self];
}
#end
#interface imageTableViewController ()
<CustomCellDelegate>
#end
#implementation imageTableViewController {
UITableView *mainTable;
NSMutableArray *imageArray;
UIButton *imageButton;
UIView *popUpView;
BOOL isFullScreen;
CGRect prevFrame;
}
- (void)viewDidLoad {
[super viewDidLoad];
imageArray = [[NSMutableArray alloc] initWithObjects:#"flower.jpeg",#"Bird.jpg",#"Browser.jpeg", nil];
mainTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, 320, 480)];
mainTable.dataSource = self;
mainTable.delegate = self;
[self.view addSubview:mainTable];
popUpView = [[UIView alloc] initWithFrame:CGRectMake(30, 30, 25, 25)];
popUpView.backgroundColor = [UIColor orangeColor];
popUpView.hidden = YES;
[mainTable addSubview:popUpView];
UIView * imageView = [[UIView alloc] initWithFrame:CGRectMake(5, 5, 20, 20)];
imageView.backgroundColor = [UIColor colorWithRed:255./255. green:0 blue:0 alpha:1];
[popUpView addSubview:imageView];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return imageArray.count;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"cell";
CustomCell *Cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (Cell == nil)
{
Cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
Cell.delegate = self;
}
Cell.imageName = [imageArray objectAtIndex:indexPath.row];
return Cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 60;
}
-(void)imgToFullScreen:(NSString *)imageName fromRect:(CGRect)frame {
if (!isFullScreen) {
[popUpView setFrame:frame];
[popUpView setHidden:NO];
[UIView animateWithDuration:0.5 delay:0 options:0 animations:^{
[popUpView setFrame:CGRectMake(120, 50, 150, 150)];
}completion:^(BOOL finished){
isFullScreen = YES;
prevFrame = frame;
}];
return;
}
else{
[UIView animateWithDuration:0.5 delay:0 options:0 animations:^{
[popUpView setFrame:prevFrame];
}completion:^(BOOL finished){
isFullScreen = NO;
popUpView.hidden = YES;
}];
return;
}
}
#pragma mark - CustomCellDelegate
- (void)didSelectImageNamed:(NSString *)name fromCell:(CustomCell *)cell {
CGRect imageFrame = cell.imageRect;
CGRect imageFrameGlobalCoord = [mainTable convertRect:imageFrame fromView:cell];
[self imgToFullScreen:name fromRect:imageFrameGlobalCoord];
}
#end

UITableViewCell subclass

I have this code segment:
if (cell == nil)
{
CGRect cellFrame = CGRectMake(0,0,300,250);
cell = [[UITableViewCell alloc] initWithFrame:cellFrame
reuseIdentifier:CellTableIndetifier];
CGRect nameLabelRect = CGRectMake(0, 5, 70, 20);
UILabel* nameLabel = [[UILabel alloc] initWithFrame:nameLabelRect];
nameLabel.textAlignment = NSTextAlignmentCenter;
nameLabel.text = #"Name";
nameLabel.font = [UIFont boldSystemFontOfSize:12];
[cell.contentView addSubview: nameLabel];
CGRect colorLabelRect = CGRectMake(0, 25, 70, 20);
UILabel* colorLabel = [[UILabel alloc] initWithFrame:colorLabelRect];
colorLabel.textAlignment = NSTextAlignmentCenter;
colorLabel.text = #"Color";
colorLabel.font = [UIFont boldSystemFontOfSize:12];
[cell.contentView addSubview: colorLabel];
CGRect priceLabelRect = CGRectMake(0, 45, 70, 20);
UILabel *priceLabel = [[UILabel alloc] initWithFrame:priceLabelRect];
priceLabel.text = #"Price";
priceLabel.textAlignment = NSTextAlignmentCenter;
colorLabel.font = [UIFont boldSystemFontOfSize:12];
[cell.contentView addSubview:priceLabel];
CGRect nameValueRect = CGRectMake(80, 5, 200, 20);
UILabel* nameValue = [[UILabel alloc] initWithFrame: nameValueRect];
nameValue.tag = kNameValueTag;
[cell.contentView addSubview:nameValue];
CGRect colorValueRect = CGRectMake(80, 25, 200, 20);
UILabel* colorValue = [[UILabel alloc] initWithFrame:colorValueRect];
colorValue.tag = kColorValueTag;
[cell.contentView addSubview:colorValue];
CGRect priceValueRect = CGRectMake(80, 45, 200, 20);
UILabel *priceValue = [[UILabel alloc] initWithFrame:priceValueRect];
priceValue.tag = kPriceValueTag;
[cell.contentView addSubview:priceValue];
}
and I would like to make that make that into a subclass, so I don't have to write all those lines, I just say cell = CustomCell and it does everything in the subclass.
Here is the basic code for a subclass of UITableCellView :
#import <UIKit/UIKit.h>
#interface CustomCell : UITableViewCell
{
}
#end
-----------------------------------------------------------
#import "CustomCell.h"
#implementation CustomCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
-(void)layoutSubviews{
[super layoutSubviews];
}
/*
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}*/
#end
It is auto-generated if you create a new file of type Objective-C Class and specify UITableViewCell in filed subclass of
Following is what I usually do. If you use the cell only in 1 view controller, you can just put it in the same file as the view controller.
#interface MyCell : UITableViewCell
#property (strong, nonatomic) UILabel* nameValue;
#property (strong, nonatomic) UILabel* colorValue;
#property (strong, nonatomic) UILabel* priceValue;
#end
#implementation MyCell
-(id)init {
self = [super initWithStyle:whatever_style];
// Create & position UI elements
UILabel* nameLabel = [[UILabel alloc] init];
nameLabel.frame = .... // frame, font, etc
[self.contentView addSubview:nameLabel]
self.nameValue = [[UILabel alloc] init];
self.nameValue = .... // frame, font, etc
[self.contentView addSubview:self.nameValue];
// Do the same thing for color, price
return self;
}
#end
By exposing nameValue, colorValue, priceValue, I allow them to be changed from outside (ie the UITableViewController). I didn't expose other labels because they are static. Unless you need special positioning, you don't have to override layoutSubviews. autoresizingMask is sufficient in most cases.
There is two way I use to solve this.
The "quick and dirty" is to design a UITableViewCell into your UITableView with the stuff you need (UILabel, UIImageView,...) and set a unique tag for each element, then when you dequeue a UITableViewCell you can reuse the elements in like this :
UILabel *nameLabel = (UILabel*)[cell viewWithTag:NAME_LABEL_TAG];
if(!nameLabel) {
// If the label does not exist, create it
CGRect nameLabelRect = CGRectMake(0, 5, 70, 20);
nameLabel = [[UILabel alloc] initWithFrame:nameLabelRect];
nameLabel.textAlignment = NSTextAlignmentCenter;
nameLabel.text = #"Name";
nameLabel.font = [UIFont boldSystemFontOfSize:12];
[cell.contentView addSubview: nameLabel];
}
Or the (imo) best way is to create a custom UITableViewCell and subclass UItableviewCell, you have a good tutorial there : Custom UITableViewCell
I guess your are putting this stuff in your cellForRowAtIndexPath: delegate method and I can see why your strive to remove it from this place.
Create a new Objective-C class via New->File and put the subview related calls you posted in the layoutSubviews: method. In cellForRowAtIndexPath: in your table view delegate now use this class instead of the generic UITableViewCell. Don't forget to import your newly created file.
#import "CellVideo.h"
#implementation CellVideo
-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
return self;
}
-(id)initWithCoder:(NSCoder *)aDecoder
{
NSLog(#"initWithCoder");
self = [super initWithCoder: aDecoder];
if (self)
{
// Initialization code
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] init];
[moviePlayer.view setFrame:CGRectMake(10, 75, 300, 260)];
[moviePlayer.view setBackgroundColor:[UIColor blackColor]];
[moviePlayer.view setTag:333];
[moviePlayer setControlStyle:MPMovieControlStyleNone];
moviePlayer.scalingMode = MPMovieScalingModeFill;
_movie=moviePlayer;
UIImageView *imagrViewThumb=[[UIImageView alloc]initWithFrame:CGRectMake(10, 75, 300, 260)];
[imagrViewThumb setBackgroundColor:[UIColor redColor]];
[imagrViewThumb setTag:333];
[self.contentView insertSubview:imagrViewThumb atIndex:0];
}
return self;
}
-(void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
///use it in this way
CellIdentifier=#"cellvideo";
UITableViewCell *cell=nil;
// CellVideo *cellVideo=nil;
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

Login page with a small list

i have just started learning objective C. want to create a small app where a login page should be there and password should be accepted only if its length is more than or equal to 8. once you submit that the next page should be a list of movies and in that page there should be a search button as well as add and remove buttons which can make that list editable. i created the login page and i have directly linked the submit button with the movie list page coz i was confused about how the condition of password length and username thing should be validated. kindly help me in doing that and to get the proper editing buttons for my movie list page.
Thanks
"ViewController.m"
#import "ViewController.h"
#import "MovieList.h"
#interface ViewController ()
#end
#implementation ViewController
-(void)loadView{
self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
self.view.backgroundColor = [UIColor cyanColor];
UILabel *name=[[UILabel alloc] init];
UILabel *pass=[[UILabel alloc] init];
UITextField *username = [[UITextField alloc]init];
UITextField *password = [[UITextField alloc]init];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[name setBackgroundColor:[UIColor cyanColor]];
[name setText:#"UserID"];
[name setTextColor:[UIColor blackColor]];
[pass setBackgroundColor:[UIColor cyanColor]];
[pass setText:#"Password"];
[pass setTextColor:[UIColor blackColor]];
[button setTitle:#"SUBMIT" forState:UIControlStateNormal];
username.delegate = self;
password.delegate = self;
username.borderStyle = UITextBorderStyleRoundedRect;
password.borderStyle = UITextBorderStyleRoundedRect;
name.frame = CGRectMake(20, 15, 80, 20);
pass.frame = CGRectMake(15, 55, 80, 20);
username.frame = CGRectMake(100, 10, 200, 30);
password.frame = CGRectMake(100, 50, 200, 30);
button.frame = CGRectMake(130, 90, 80, 30);
[self.view addSubview:button];
[self.view addSubview:username];
[self.view addSubview:password];
[self.view addSubview:name];
[self.view addSubview:pass];
[button addTarget:self action:#selector(gotosecondpage) forControlEvents:UIControlEventTouchUpInside];
}
-(void)gotosecondpage
{
Movielist *secondViewcont = [[Movielist alloc] init];
[self.navigationController pushViewController:secondViewcont animated:YES];
}
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)viewDidUnload
{
[super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
return YES;
}
- (BOOL)textFieldShouldClear:(UITextField *)textField{
return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
#end
"MovieList.m"
#import "MovieList.h"
#interface Movielist()
#end
#implementation Movielist
-(void)loadView{
self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
self.view.backgroundColor = [UIColor cyanColor];
}
-(id)init{
[self.navigationItem setTitle:#"Movie List"];
return self;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
[self.view setBackgroundColor:[UIColor cyanColor]];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
CGRect frame = self.view.frame;
UITableView *tableView = [[UITableView alloc] initWithFrame:frame style:UITableViewStylePlain];
tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
tableView.backgroundColor = [UIColor cyanColor];
tableView.delegate = self;
tableView.dataSource = self;
[self.view addSubview:tableView];
[tableView setScrollEnabled:YES];
[tableView setUserInteractionEnabled:YES];
[tableView setRowHeight:35];
}
- (void)viewDidUnload
{
[super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 20;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"cellIDent"];
char ch = 'a' + indexPath.row;
cell.textLabel.text = [NSString stringWithFormat:#"%c" , ch];
return cell;
}
#end
Checking length is very straight. The important thing is how do you navigate from login page. Currently I see gotosecondpage function is responsible. You can probably write a validateParams method, under which you can call gotosecondpage if all conditions are met.
This code will tell you about password length:
-(void) validateParams
{
NSString * password = [password text];
if ([password length] < 8) //and maybe other conditions, too
{
[self gotosecondpage];
}
else
{
//report error - maybe show some UILabel near username / password fields to show which one of them went wrong
}
}
Note that there are many other things you can do - like telling the user which field needs correction, and there are many better ways. Since your requirements are quite basic, above should help for now.

Resources