1 设置委托(代理模式)
具体在xcode中新建一个Utility Application 模板的工程,里面就算采用代理设计模式
2 extern (外部变量)
interface之前定义extern NSString *strZhi;
.h
extern NSString *strZhi;
@interface OneViewController : UIViewController {
}
@end
.m
NSString *strZhi=@"额的神阿,传值了";
@implementation OneViewController
3 NSUserDefaults
.h
#define myAvgcost @"myavgcost"
.m
[[NSUserDefaults standardUserDefaults] setObject:p_Avgcost_arr forKey:myAvgcost];//写到磁盘
传到另一个类
NSArry *myAvgcost_arr=[[NSUserDefaults standardUserDefaults] arrayForKey:myAvgcost];
4 单例
主要是全局共享一个类时主要采用的方式。
http://blog.csdn.net/cocoa_geforce/article/details/6747595
5delegate
主要是要把共享的数据设置为XXDelegate的一个成员变量;
然后在需要的地方通过如下方法获得一个XXDelegate的对象,如下:
XXDelegate*app = (XXDelegate *) [[UIApplicationsharedApplication] delegate];
每次获取delegate并不是创建一个新app,每个app只有一个delegate。
实现功能:第一个页面点击button,进入第二个页面,选中一条记录,按select按钮,把值传回第一个页面的textfield
//
// ViewController.m
// demo1
//
// Created by jessewang on 11/29/11.
// Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"back" style:UIBarButtonItemStyleBordered target:nil action:nil];
self.navigationItem.backBarButtonItem = backButton;
[backButton release];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(showOutput:) name:@"myNotification"object:nil];
}
-(void) showOutput:(NSNotification *)note
{
NSLog(@"%@",note);
self.output.text = note.object;
}
//
// NextView.m
// demo1
//
// Created by jessewang on 11/29/11.
// Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
NSArray *myarray = [[NSArray alloc] initWithObjects:@"A",@"B",@"C",@"D",@"E",nil];
self.array = myarray;
[myarray release];
UIBarButtonItem *selectButton = [[UIBarButtonItem alloc] initWithTitle:@"select" style:UIBarButtonItemStyleBordered target:self action:@selector(selectButton:)];
self.navigationItem.rightBarButtonItem = selectButton;
[selectButton release];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
self.lastIndexPath = nil;
self.array = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return [self.array count];
}
// Customize the appearance of table view cells.
- (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]autorelease];
}
NSUInteger row = [indexPath row];
NSUInteger oldRow = [self.lastIndexPath row];
cell.textLabel.text = [array objectAtIndex:row];
cell.accessoryType = (row == oldRow && lastIndexPath != nil) ? UITableViewCellAccessoryCheckmark:UITableViewCellAccessoryNone;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//choose one
int newRow = [indexPath row];
int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1;
if(newRow != oldRow)
{
UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];
newCell.accessoryType = UITableViewCellAccessoryCheckmark;
UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:lastIndexPath];
oldCell.accessoryType = UITableViewCellAccessoryNone;
self.value = [self.array objectAtIndex:[indexPath row]];
self.lastIndexPath = indexPath;
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
-(IBAction)selectButton:(id)sender
{
[[NSNotificationCenter defaultCenter] postNotificationName:@"myNotification" object:self.value];
[self.navigationController popViewControllerAnimated:YES];
}
7 对象的property属性传值
8 方法的参数传值
9 静态方法传值
10 用给 指向 指针的指针 赋值 的方式传值