将NSString对象添加到另一个ViewController

时间:2013-04-26 08:11:40

标签: nsmutablearray uitableview parameter-passing

首先我在这里检查了很多帖子,但我找不到合适的解决方案。我是ObjC的新手,所以我自己无法找到解决方案! 问题: 我有一个带有2个Tab的TabBArController,每个Tab都包含一个TableviewController。从我的第一个ViewController开始,我试着将不同的Url字符串添加到我的SecondTableViewController的数组中。

来自FirstviewController的方法,它应该将url添加到SecondViewController的数组中:

- (IBAction)setJobs:(id)setJobs
{
    NSString *parameterString = [[NSString alloc]init];
    for (UITextField*textField in textFields)
    {
        parameterString = [parameterString stringByAppendingString:[NSString    stringWithFormat:@"%@,",textField.text]];
    }

    NSString*user = [[NSString alloc]init];
    NSString*password = [[NSString alloc]init];
    NSString*firmenId = [[NSString alloc]init];

    user = [[NSUserDefaults standardUserDefaults] objectForKey:@"Username"];
    password = [[NSUserDefaults standardUserDefaults] objectForKey:@"Password"];
    firmenId = [[NSUserDefaults standardUserDefaults] objectForKey:@"FirmenId"];

    NSString*urlString=[[NSString alloc]initWithFormat:@"http://www.someurl.com/firmenid=%@;projectid=%@;user=%@;kennwort=%@;appparminfo=%@",firmenId,[self.detailRowArray objectAtIndex:1],user,password,parameterString];

    [projektsInUse addObject:urlString];


    // Here the url should be passed to the other Viewcontroller
    TableAnswerController *tAVC=[[TableAnswerController alloc]init];
    [tAVC.tabbleArray addObject:urlString];



    NSURLRequest * request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:15.0];
    NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:self];
    if(connection)
    {
        //connection didReceiveData
    }
    else
    {
        // Error Handling
    }

}

这是我的SecondTableViewController:

#import "TableAnswerController.h"

@interface TableAnswerController ()

@end

@implementation TableAnswerController

@synthesize timer,tabbleArray;



- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) 
    {

    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    if(!self.tabbleArray)
    {
        self.tabbleArray = [NSMutableArray array];
    }
}
- (IBAction)refreshAnswers:(id)sender
{
    NSLog(@"TableArray: %@",tabbleArray);

}

感谢您的时间!!

1 个答案:

答案 0 :(得分:0)

您需要先初始化数组

TableAnswerController *tAVC=[[TableAnswerController alloc]init];
 tAVC = [NSMutableArray new];
[tAVC.tabbleArray addObject:urlString];

OR

NSArray *arr = [[NSArray alloc]initWithObjects:urlString, nil];
[tAVC setTabbleArr:arr];