iOS通过TabView将值传递给视图控制器

时间:2013-07-01 07:54:02

标签: ios uiviewcontroller return-value uitabview

创建了登录页面,经过身份验证后,用户可以访问选项卡视图。想问一下是否有可能传递一个包含URL的字符串值到选项卡的第一个视图,同时显示选项卡的视图?

Image

以下是我的代码,

LoginViewController.m

//
//  LoginViewController.m
//  LoginJson
//
//  Created by Ng Zhi Wei on 26/6/13.
//  Copyright (c) 2013 Ng Zhi Wei. All rights reserved.
//

#import "LoginViewController.h"
#import "PhotoViewController.h"
#import "JSONKit.h"

@interface LoginViewController ()

@end

@implementation LoginViewController

NSString *userNameGlobal;
NSString *passwordGlobal;
NSString *photoUrlGlobal;

NSMutableArray *array = nil;  //added
NSMutableData *responseData = nil;  //added

@synthesize todoItems;  //added

- (void)viewDidLoad
{`enter code here`
    // Begin Add

    // URL that calls into the Azure Service`enter code here`
    NSString *serviceUri = @"https://excubantmobileservice.azure-mobile.net/tables/UserInfo";

    // Convert to NSURL type
    NSURL *myURL = [NSURL URLWithString:serviceUri];
    // Create a request object
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:myURL
                                                           cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
                                                       timeoutInterval:60];

    // Modify http headers for GET request
    [request setHTTPMethod: @"GET"];
    // Indicate JSON Data Format
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    // Indicate host property
    [request setValue:@"excubantmobileservice.azure-mobile.net" forHTTPHeaderField:@"Host"];
    // Indicate application key (you get this from the portal)
    [request setValue:@"EMhqrMsBajSIlVuOYLuOJRQoGHHImu90" forHTTPHeaderField:@"X-ZUMO-APPLICATION"];
    // Execute request
    [NSURLConnection connectionWithRequest:request delegate:self];
    // Allocate list of results to TableViewControl
    self.todoItems = [[NSMutableArray alloc] initWithArray:array];
    // End Add

    [super viewDidLoad];

}

// Add this code for the responseData processing
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    responseData = [[NSMutableData alloc] init];
}

// Add this code for the responseData processing
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [responseData appendData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    // Some debugging code
    NSLog(@"Succeeded! Received %d bytes of data",[responseData
                                                   length]);
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void) alertStatus:(NSString *)msg :(NSString *)title
{
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title
                                                        message:msg
                                                       delegate:self
                                              cancelButtonTitle:@"Ok"
                                              otherButtonTitles:nil, nil];

    [alertView show];
}


- (IBAction)loginClicked:(id)sender {
    // Create an array from the response data
    NSArray *itemArray = [responseData objectFromJSONData];
    Boolean isFound = false;
    NSString *data1 = nil;
    NSString *data2 = nil;
    NSString *data3 = nil;
    array = [[NSMutableArray alloc] init];
    // Loop through array of results, pulling out the "text" column
    for(NSDictionary * dataDict in itemArray)
    {
        // Build up array. Later we'll append array to
        // TableView control

        data1 = [dataDict objectForKey:@"userId"];
        [array addObject:data1];

        data2 = [dataDict objectForKey:@"password"];
        [array addObject:data2];

        data3 = [dataDict objectForKey:@"photoUrl"];
        [array addObject:data3];

        if ([[_txtUserName text] isEqualToString:data1] && [[_txtPassword text] isEqualToString:data2] ) {
            userNameGlobal = data1;
            passwordGlobal = data2;
            photoUrlGlobal = data3;
        }
    }
        if ([[_txtUserName text] isEqualToString:userNameGlobal] && [[_txtPassword text] isEqualToString:passwordGlobal] ) {
            [self alertStatus:@"Welcome!" :@"Login Success!"];
            NSLog(photoUrlGlobal);
            isFound = true;
            UIStoryboard *mainStoryBoard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:nil];
            UITabBarController *vc = [mainStoryBoard instantiateViewControllerWithIdentifier:@"photoPush"];
            [self presentViewController:vc animated:YES completion:nil];
        }
    if (!isFound) {
        [self alertStatus:@"Incorrect Username & Password!" :@"Login Failure!"];
    }
}

- (IBAction)backgroundClick:(id)sender {
    [_txtPassword resignFirstResponder];
    [_txtUserName resignFirstResponder];
}

@end

PhotoViewController.m
//
//  PhotoViewController.m
//  MJPEG
//
//  Created by Ng Zhi Wei on 10/6/13.
//  Copyright (c) 2013 cflim. All rights reserved.
//

#import "PhotoViewController.h"

@interface PhotoViewController ()

@end

@implementation PhotoViewController

@synthesize photoUrl;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    NSString *fullURL = photoUrl;
    NSURL *url = [NSURL URLWithString:fullURL];
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
    [_photoView loadRequest:requestObj];
    NSLog(fullURL);
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (IBAction)refreshPage:(id)sender {
    [_photoView reload];
}


@end

1 个答案:

答案 0 :(得分:0)

试试这个:

  • 向AppDelegate添加名为url
  • 的属性
  • 为您的UIViewController创建一个扩展类,让您获取/设置网址

e.g。类扩展

@implementation UIViewController (AppDelegate)
- (NSString *) url {
    return [(NSPAppDelegate *)[[UIApplication sharedApplication] delegate] url];
}

- (void) setUrl:(NSString*)value {
    [(NSPAppDelegate *)[[UIApplication sharedApplication] delegate] setAuthenticated:value];
}
@end

现在您有一个可以在应用中的任何位置访问的属性。设置网址后,您只需导航回选项卡并重新绘制视图即可!