Reputation: 606
I have a label on my main View. I have a button which brings me to another view. On it I have a button to change my label which is on the main view.
How can I do this? I included a projet, could someone help me please...
Upvotes: 1
Views: 817
Reputation: 19869
I usually use a delegate protocol:
in the second view h add :
@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
in the second view m file call:
[delegate quickViewController:self
stringForLabel:@"your string"];
in the main view h file add: , like that:
@interface MainView:UIViewController<viewControllerDelegate>
in the main view m file:
first: when you init the second view don't forget to add:
secondview.delegate = self;
other wise it wont work.
second: add the delegate function:
- (void)viewController:(ViewController*)controller stringForLabel:(NSString*)string{
//set the label from the string passed
lable.text = string;
}
hope it will help shani
Upvotes: 5
Reputation: 19869
O.K i usually don't do that but for now...
this are your files after i have change them -
View1 .h (try to name those files with a capital letter);
#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
Thats it. by the way: you had many small mistakes that i have fixed. look at the fixes to try to understand them.
Upvotes: 3