-1

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

图片

这是我的代码,

登录视图控制器.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
4

1 回答 1

0

尝试这个:

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

例如类扩展

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

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

现在您有了一个可以从应用程序中的任何位置访问的属性。设置 url 后,您可以导航回您的选项卡并重新绘制视图!

于 2013-07-11T14:08:36.737 回答