开发者

sending string between views

i have 2 views i m sending text in a button on 1st view to label on second view....

//////textfieldtolabelViewController.h

#import <UIKit/UIKit.h>
#import "seconview.h"

@interface textfieldtolabelViewController : UIViewController {
    IBOutlet seconview *sec;
    //IBOutlet UITextField *t1;
}

//@property(nonatomic, retain)IBOutlet UITextField *t1;
-(void)buttonclick:(id)sender;
@end

and its .m file is this

#import "textfieldtolabelViewController.h"

@implementation textfieldtolabelViewController


-(void)buttonclick:(id)sender
{
    NSString *s = [sender titleForState:UIControlStateNormal];  
    //sec.ss = s;
    [sec settext:s];
    [self presentModalViewController:sec animated:YES]; 
}
- (void)dealloc {
    [super dealloc];
}


@end

now there is second view naming seconview .h file

#import <UIKit/UIKit.h>



@interface seconview : UIViewController {
    IBOutlet UILabel *l1;

}
@property(nonatomic, retain)IBOutlet UILabel *l1;


-(void)settext:(NSString *)ss;
@end

and its .m file is ....

#import "seconview.h"


@implementation seconview
@synthesize l1;
//@synthesize ss;

-(void)settext:(NSString 开发者_如何转开发*)ss
{
    l1.text=ss; 
}


- (void)dealloc {
    [super dealloc];
}


@end

this program did not show any error and runs fine but problem is that the text of button in 1st view does not appear in label on second view but i made all connection perfectly.......


Most probably you didn't connect l1 to the actual label in the Interface Builder. Verify with the debugger, that l1 is not nil in your settext method


You can include the view controller containing the label in the view controller where you want it to change, and access it by allocating a copy of it.

For example, use something like this in textfieldtolabelViewController.m

#import "seconview.h"

-(void)buttonclick:(id)sender
{
    NSString *s = [sender titleForState:UIControlStateNormal];
    seconview *viewcontroller = [[seconview alloc] initWithNibName:@"seconview" bundle:nil];
    [[viewcontroller l1] setText:s];
    [viewcontroller release];
    [self presentModalViewController:sec animated:YES]; 
}


the [self presentModalViewController:sec animated:YES]; should be above the setText try it

-(void)buttonclick:(id)sender
{
    NSString *s = [sender titleForState:UIControlStateNormal];  
    //sec.ss = s;

    [self presentModalViewController:sec animated:YES];


    [sec settext:s];

}

hope it will work....

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜