答案 0 :(得分:5)
我通常使用委托协议:
在第二个视图中添加:
@protocol viewControllerDelegate;
@interface viewController : UIView
id < viewControllerDelegate > delegate;
@property (nonatomic, assign) id < QuickViewControllerDelegate > delegate;
@end
@protocol viewControllerDelegate
- (void)viewController:(ViewController *)controller stringForLabel:(NSString*)string;
@end
在第二个视图m文件调用中:
[delegate quickViewController:self
stringForLabel:@"your string"];
在主视图h文件中添加:,类似:
@interface MainView:UIViewController<viewControllerDelegate>
主视图m文件中的:
首先:当你初始化第二个视图时,不要忘记添加:
secondview.delegate = self;
另外,它不会工作。
第二:添加委托功能:
- (void)viewController:(ViewController*)controller stringForLabel:(NSString*)string{
//set the label from the string passed
lable.text = string;
}
希望它会有所帮助
SHANI
答案 1 :(得分:3)
O.K我通常不这样做,但现在......
这是我更改后的文件 -
View1 .h (尝试使用大写字母命名这些文件);
#import <UIKit/UIKit.h>
@protocol View1Delegate;
@interface View1 : UIViewController {
id <View1Delegate> delegate;
IBOutlet UIButton *btn_changelbl;
IBOutlet UIButton *btn_back;
}
@property (nonatomic, assign) id <View1Delegate> delegate;
@property (nonatomic,retain) IBOutlet UIButton *btn_changelbl;
@property (nonatomic,retain) IBOutlet UIButton *btn_back;
-(IBAction) backToMain:(id)sender;
-(IBAction) changeLabel:(id)sender;
@end
@protocol View1Delegate
- (void)view1:(View1*)controller labelNeedsChage:(BOOL)needsChange stringForLabel:(NSString*)string;
@end
<强> View1.m 强>
#import "View1.h"
#import "testViewController.h"
@implementation View1
@synthesize delegate;
@synthesize btn_changelbl;
@synthesize btn_back;
-(IBAction) backToMain:(id)sender {
[self dismissModalViewControllerAnimated:YES];
[delegate view1:self labelNeedsChage:NO stringForLabel:nil];
}
-(IBAction) changeLabel:(id)sender{
[delegate view1:self labelNeedsChage:YES stringForLabel:@"new text"];
[self dismissModalViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
[super viewDidUnload];
}
- (void)dealloc {
[btn_changelbl release];
[btn_back release];
[super dealloc];
}
@end
<强> TestViewController.h 强>
#import <UIKit/UIKit.h>
#import "View1.h"
@interface testViewController : UIViewController<View1Delegate> {
IBOutlet UILabel *lb_test;
IBOutlet UIButton *btn_changeView;
}
@property (nonatomic,retain) IBOutlet UILabel *lb_test;
@property (nonatomic,retain) IBOutlet UIButton *btn_changeView;
-(IBAction) changeView:(id)sender;
@end
<强> TestViewController.m 强>
#import "testViewController.h"
#import "view1.h"
@implementation testViewController
@synthesize lb_test;
@synthesize btn_changeView;
-(IBAction) changeView:(id)sender {
View1 *myView = [[View1 alloc] init];
myView.delegate=self;
[self presentModalViewController:myView animated:YES];
}
- (void)view1:(View1*)controller labelNeedsChage:(BOOL)needsChange stringForLabel:(NSString*)string{
if(needsChange){
lb_test.text=string;
}
}
- (void)dealloc {
[lb_test release];
[btn_changeView release];
[super dealloc];
}
@end
多数民众赞成。 顺便说说: 你有很多我犯过的小错误。查看修复程序以尝试理解它们。