简体   繁体   中英

Pass string from UITableView to UIView in iOS

I want to pass 2 string selected from UITableview (CompleteView) to new UIView (DetailView) This is my code:

In CompleteView.h()

@property (nonatomic, retain) NSString *memoString;
@property (nonatomic, retain) NSString *previewString;

In Completeview.m()

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{           
previewString = [PreviewArray objectAtIndex:indexPath.row];
            NSLog(@"Preview string %@",previewString);
            memoString = [MemoArray objectAtIndex:indexPath.row];
            NSLog(@"Memo string %@",memoString);

            DetailView *detailAlert = [[DetailView alloc] initWithFrame:CGRectMake(10, 40, 300, 300)];
            detailAlert.strPreview =previewString ;
            detailAlert.strMemo =  memoString;

            [self.view addSubview:detailAlert];
            [detailAlert show];
            [detailAlert release];
}

In DetailView()

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        originalFrame = frame;

        UIWebView *videoview = [[UIWebView alloc] initWithFrame:CGRectMake(10,80,275.0,150)];
        NSString *url=[NSURL URLWithString:strPreview];
        NSURL *nsurl=[NSURL URLWithString:url];
        NSURLRequest *nsrequest=[NSURLRequest requestWithURL:nsurl];
        [videoview loadRequest:nsrequest];
        [self addSubview:videoview];

    return self;
}

In DetailView.h

@property (nonatomic, retain) NSString* strMemo;
@property (nonatomic, retain) NSString* strPreview;

When run, previewString not send to DetailView. I'm debug, when run strPreview and strMemo in DetailView is 0x00000. Thanks in advance

try this:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {           
    previewString = [PreviewArray objectAtIndex:indexPath.row];
    NSLog(@"Preview string %@",previewString);
    memoString = [MemoArray objectAtIndex:indexPath.row];
    NSLog(@"Memo string %@",memoString);

    DetailView *detailAlert = [[DetailView alloc] initWithFrame:CGRectMake(10, 40, 300, 300)];
    detailAlert.strPreview =previewString ;
    detailAlert.strMemo =  memoString;
    [detailAlert createWebView];

    [self.view addSubview:detailAlert];
    [detailAlert show];
    [detailAlert release];

}

write code for string allocation

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        originalFrame = frame;

        //add following two lines
        strPreview = [[NSString alloc] init];
        strMemo = [[NSString alloc] init];
        //........
        return self;
}

-(void) createWebView {
    UIWebView *videoview = [[UIWebView alloc] initWithFrame:CGRectMake(10,80,275.0,150)];
    NSString *url=[NSURL URLWithString:strPreview];
    NSURL *nsurl=[NSURL URLWithString:url];
    NSURLRequest *nsrequest=[NSURLRequest requestWithURL:nsurl];
    [videoview loadRequest:nsrequest];
    [self addSubview:videoview];
}

The problem here is you are setting two properties in detail view after initializing DetailView. But you are trying to use a property values within the initializer which are yet to be set.

As I can see there are two options you can try out.

  1. Write another initializer which accept these two properties. So that you can use those two properties in that initializer.

    • (id)initWithFrame:(CGRect)frame strMemo:(NSString *)memo strPreview:(NSString *)preview { self = [super initWithFrame:frame]; if (self) { originalFrame = frame;

       UIWebView *videoview = [[UIWebView alloc] initWithFrame:CGRectMake(10,80,275.0,150)]; NSString *url=[NSURL URLWithString:preview]; NSURL *nsurl=[NSURL URLWithString:url]; NSURLRequest *nsrequest=[NSURLRequest requestWithURL:nsurl]; [videoview loadRequest:nsrequest]; [self addSubview:videoview]; return self; 

      }

  2. Or else you can extract the logic in the current initializer and put them in to a separate method which you can called after setting two properties from the CompleteView.

You can pass custom Initialisers like below . If you wish you can add one parameter to custom Initialisers

In CompleteView.h()

@property (nonatomic, retain) NSString *memoString;
@property (nonatomic, retain) NSString *previewString;

In Completeview.m()

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{           
previewString = [PreviewArray objectAtIndex:indexPath.row];
            NSLog(@"Preview string %@",previewString);
            memoString = [MemoArray objectAtIndex:indexPath.row];
            NSLog(@"Memo string %@",memoString);

            DetailView *detailAlert = [[DetailView alloc] initWithFrame:CGRectMake(10, 40, 300, 300) andPreviewstring:previewString];
            detailAlert.strPreview =previewString ;
            detailAlert.strMemo =  memoString;

            [self.view addSubview:detailAlert];
            [detailAlert show];
            [detailAlert release];
}

In DetailView()

- (id)initWithFrame:(CGRect)frame andPreviewstring:(NSString *)stPreview
{
    self = [super initWithFrame:frame];
    if (self) {
        originalFrame = frame;

        UIWebView *videoview = [[UIWebView alloc] initWithFrame:CGRectMake(10,80,275.0,150)];
        NSString *url=[NSURL URLWithString:strPreview];
        NSURL *nsurl=[NSURL URLWithString:url];
        NSURLRequest *nsrequest=[NSURLRequest requestWithURL:nsurl];
        [videoview loadRequest:nsrequest];
        [self addSubview:videoview];

    return self;
}

In DetailView.h

@property (nonatomic, retain) NSString* strMemo;
@property (nonatomic, retain) NSString* strPreview;
- (id)initWithFrame:(CGRect)frame andPreviewstring:(NSString *)stPreview;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM