This tutorial is used to show how to create the custom views in objective c.
1. Open the xcode & choose "File->New Project".
2. Select "window-based-Aapplication" from left menu .
3. Name your project as "Untitled" and save the project.
4. Then select "UntitledAppDelegate.h" file from the left side menu or group and files. Then enter the following :
#import <UIKit/UIKit.h>
#import "DetailView.h"
@interface UntitledAppDelegate : NSObject <UIApplicationDelegate, UITableViewDelegate ,UITableViewDataSource> {
UIWindow *window;
NSArray *country , *capital ,*images;
UIView *view;
DetailView *viewController;
}
-(UntitledAppDelegate *)set:(int) a;
@property(retain,nonatomic)NSArray *country,*capital,*images;
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property(retain,nonatomic)UIView *view;
@end
5. Then select "UntitledAppDelegate.m" file from the left side menu or group and files.
#import "UntitledAppDelegate.h"
#import "DetailView.h"
@implementation UntitledAppDelegate
@synthesize window,country,capital,images,view;
#pragma mark -
#pragma mark Application lifecycle
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSArray *country1 = [[NSArray alloc] initWithObjects:@"India",@"Pakistan",@"Srilanka",@"China",nil];
capital = [[NSArray alloc]initWithObjects:@"Delhi",@"Karachi",@"Columbo",@"Beejing",nil];
images = [[NSArray alloc]initWithObjects:@"india.jpg",@"pakistan.jpg",@"srilanka.jpeg",@"china.jpeg",nil];
self.country = country1;
view = [[UIView alloc]initWithFrame:CGRectMake(0,0,320,480)];
UINavigationBar *navigationBar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0,10,320,50)];
navigationBar.barStyle = UIBarStyleBlackOpaque;
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(80,0, 150,40)];
label.text = @"Custom Table";
label.textAlignment =UITextAlignmentCenter;
label.baselineAdjustment= UIBaselineAdjustmentAlignCenters;
label.backgroundColor = [UIColor blackColor];
label.textColor =[UIColor whiteColor];
[navigationBar addSubview:label];
/
[view addSubview:navigationBar];
UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0,50,320,480) style:UITableViewStylePlain];
tableView.dataSource =self;
tableView.delegate = self;
[view addSubview:tableView];
[window addSubview:view];
[self.window makeKeyAndVisible];
return YES;
}
6. We need to create methods provided by the UITableView in order to manipulate table. So define cellForRowAtIndexPath , numberOfSectionInTableView, numberofRowsInSection, didSelectRowAtIndexPath and a constructor to initialize a data for the another view.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
NSString *value =[[NSString alloc]initWithFormat:@"%@",[country objectAtIndex:indexPath.row]];
NSString *imageName = [[NSString alloc]initWithFormat:@"%@",[images objectAtIndex:indexPath.row]];
cell.textLabel.text = value;
cell.image = [UIImage imageNamed:imageName];
cell.detailTextLabel.text = [[NSString alloc]initWithFormat:@"%@",[capital objectAtIndex:indexPath.row]];
return cell;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.country count];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
viewController = [[DetailView alloc]init];
viewController.v = indexPath.row;
NSLog(@"%i",viewController.v);
[window addSubview:viewController.view];
[window makeKeyAndVisible];
}
-(UntitledAppDelegate *) set:(int)a
{
NSArray *country1 = [[NSArray alloc] initWithObjects:@"India",@"Pakistan",@"Srilanka",@"China",nil];
capital = [[NSArray alloc]initWithObjects:@"Delhi is captial of india",@"Karachi is captial of pakistan",@"Columbo is a capital of sri lanka",@"Beejing is capital of china",nil];
country = country1;
return self;
}
6. Then right click the class folder and click on the new file. Select NsObject class from the right hand side and then named it as DetailView.h without selecting xib option . Open DetailView.h and implement the following code.
#import <UIKit/UIKit.h>
@interface DetailView : UIViewController {
int v;
UIView *view;
}
@property(nonatomic,readwrite)int v;
-(void)backWard;
@end
7. Then open up the DetailView.m file from the left hand side. Add the following code in to the left hand viewDidLoad.
- (void)viewDidLoad {
NSLog(@"%i",v);
[super viewDidLoad];
UntitledAppDelegate * appDelegate = [[UntitledAppDelegate alloc]set:0];
view = [[UIView alloc]initWithFrame:CGRectMake(0,0, 320, 480)];
UINavigationBar *navigationBar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0,10,320,50)];
navigationBar.barStyle = UIBarStyleBlackOpaque;
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(80,0, 150,60)];
label.text = @"Description";
label.textAlignment =UITextAlignmentCenter;
label.baselineAdjustment= UIBaselineAdjustmentAlignCenters;
label.backgroundColor = [UIColor blackColor];
label.textColor =[UIColor whiteColor];
[navigationBar addSubview:label];
[view addSubview:navigationBar];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame=CGRectMake(10,15,50,20);
[button addTarget:self action:@selector(backWard) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:@"Back" forState:UIControlStateNormal];
button.backgroundColor =[UIColor blackColor];
[button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[navigationBar addSubview:button];
UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(10,100,100,50)];
label1.text= [appDelegate.country objectAtIndex:v];
label1.textAlignment =UITextAlignmentCenter;
label1.baselineAdjustment= UIBaselineAdjustmentAlignCenters;
[view addSubview:label1];
UITextView *textView = [[UITextView alloc]initWithFrame:CGRectMake(10,160,200,200)];
textView.text = [appDelegate.capital objectAtIndex:v];
textView.textColor =[UIColor redColor];
textView.textAlignment = UITextAlignmentCenter;
[view setAutoresizingMask:UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth];
[view setBackgroundColor:[UIColor purpleColor]];
self.view = view;
[view addSubview:textView];
[view addSubview:label];
}
-(void)backWard
{
UntitledAppDelegate *appDelegate = [[UntitledAppDelegate alloc]init];
self.view = appDelegate.view;
}
8. Then add images in to the Resource folder.
9. Click on the Built and Run tool and save it. Now you will have an output in front of you.
Ppl like you get all the brains. I just get to say tahkns for he answer.
TYFgzM hdneainswsom
r17zml , [url=http://zhqkrcefuktj.com/]zhqkrcefuktj[/url], [link=http://pahoeyidtabf.com/]pahoeyidtabf[/link], http://xobbngvcxpsf.com/
(3) Responses to this post