universal开发ipad数据传递失败

ALibera 发布于 2013/06/17 22:40
阅读 186
收藏 0
iOS

//头文件

#import <UIKit/UIKit.h>


@interface WeiboListViewController : UITableViewController

@property(nonatomic,strong)NSMutableArray *allWeibo;

@property(nonatomic, strong)UIActivityIndicatorView *activityIndicator;

@end



//master的类

#import "WeiboListViewController.h"

#import "Weibo.h"

#import "ViewController.h"

#import "AppDelegate.h"


@interface WeiboListViewController ()


@end


@implementation WeiboListViewController


- (id)initWithStyle:(UITableViewStyle)style

{

    self = [super initWithStyle:style];

    if (self) {

        // Custom initialization

    }

    return self;

}


- (void)viewDidLoad

{

    [super viewDidLoad];

    

    // waiting

    self.activityIndicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];

    self.activityIndicator.frame = CGRectMake(0.0, 0.0, 40.0, 40.0);

    self.activityIndicator.center = self.view.center;

    self.activityIndicator.hidden=NO;

    [self.view addSubview: self.activityIndicator];

    [self.activityIndicator bringSubviewToFront:self.view];

    [self.activityIndicator startAnimating];


    self.title = @"新浪微博";

    self.allWeibo = [[NSMutableArray alloc]init];

    // 开始后台下载数据任务

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

        NSData* data = [NSData dataWithContentsOfURL: [NSURL URLWithString:@"http://course.gdou.com/gxk/html/weibo2.json"] ];

        NSError* error = nil;

        NSArray *items= [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];

        if (error != nil){

            NSLog(@"");

            return;

        }

        for(NSDictionary *d in items){

            NSDictionary *user = [d valueForKey:@"user"];

            NSString *city=[user valueForKey:@"location"];

            if([city isEqualToString:@"其他"]){

                city=@"中国";

            }

            NSString *urlStr=[NSString stringWithFormat:@"http://course.gdou.com/gxk/html/hw/%@.json",city];

            NSString *escapedURL=[urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

            NSData* rootData = [NSData dataWithContentsOfURL: [NSURL URLWithString:escapedURL]];

            

            NSDictionary *arrJson= [NSJSONSerialization JSONObjectWithData:rootData options:kNilOptions error:&error];

            // 某个名字的地址,经以上接口返回的 results 可能会有多个结果,我们只取第一个

            NSArray *results=[arrJson valueForKey:@"results"];

            if(!results || results.count==0){

                NSLog(@"results return 0:%@",arrJson);

            }

            NSDictionary *geometry = [results[0] valueForKey:@"geometry"];

            // 注意 [geometry valueForKey:@"location"] 返回的是一个 immutable 的对象,不能直接使用

            NSDictionary *geom=[geometry valueForKey:@"location"];

            NSMutableDictionary *location=[NSMutableDictionary dictionaryWithDictionary:geom];

            [location setValue:city forKey:@"city"];

            

            Weibo *w=[[Weibo alloc]initWithName:[user valueForKey:@"screen_name"] andText:[d valueForKey:@"text"] andImgURL:[user valueForKey:@"profile_image_url"] andLocation:location];

            [self.allWeibo addObject:w];

            

        }

        // 数据下载完毕,在主线程中更新表格

        dispatch_async(dispatch_get_main_queue(), ^{

            [self.activityIndicator stopAnimating];

            [self.tableView reloadData];

        });

    });

    

}


- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


#pragma mark - Table view data source


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

    // Return the number of sections.

    return 1;

}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    // Return the number of rows in the section.

    return self.allWeibo.count;

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    

    // Configure the cell...

    Weibo *w = [self.allWeibo objectAtIndex:indexPath.row];

    cell.textLabel.text = w.name;

    cell.detailTextLabel.text = w.text;

    


    // 处理weibo 头像代码

    UIImage *myImage2 =[[UIImage alloc]initWithData: [NSData dataWithContentsOfURL:[NSURL URLWithString:w.imgURL]]];

    cell.imageView.image = myImage2;

    return cell;

}


#pragma mark - Table view delegate


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

    

    

    //MapViewController *v = [segue destinationViewController];

    [self performSegueWithIdentifier:@"map" sender:self];

}


-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender

{

   MapViewController *v = [segue destinationViewController];

    NSIndexPath *index = [self.tableView indexPathForSelectedRow];

    Weibo *w = self.allWeibo[index.row];

    v.location = w.location;

    //v.myAnn;

}

@end



//委托类

#import "AppDelegate.h"


@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

    // Override point for customization after application launch.

    if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)

    {

        self.splitViewController = (UISplitViewController *)self.window.rootViewController;

        self.masterViewController =[self.splitViewController.viewControllers objectAtIndex:0];

        UINavigationController *nc = [self.splitViewController.viewControllers objectAtIndex:1];

        self.detailViewController =(MapViewController *)[nc topViewController];

        self.splitViewController.delegate = self.detailViewController;

        

    }

    return YES;

}


//detail类也是iphone segue后的下一个页面头文件

#import <UIKit/UIKit.h>

#import <MapKit/MapKit.h>


@interface MapViewController : UIViewController<CLLocationManagerDelegate,UIPopoverControllerDelegate, UISplitViewControllerDelegate>


@property (weak, nonatomic) IBOutlet MKMapView *myMapView;

@property (nonatomic, strong) NSDictionary *location;

@property (strong, nonatomic) CLLocationManager *locationManager;

@property (strong, nonatomic) MKPointAnnotation *myAnn;

@property (strong, nonatomic) UIPopoverController *masterPopoverController;

@end


//.m文件

#import "MapViewController.h"


@interface MapViewController ()


@end


@implementation MapViewController


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        // Custom initialization

    }

    return self;

}


- (void)setLocation:(NSDictionary *)d

{

    _location=d;

    if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){


    }

}


- (void)viewDidLoad

{

    [super viewDidLoad];

    

   // NSDictionary *d=[NSJSONSerialization JSONObjectWithData:self.location options:kNilOptions error:nil];

    //[self.myMapView ];

    NSLog(@"localtion:%@",[self.location valueForKey:@"lat"]);

    self.title = @"地图";

// Do any additional setup after loading the view.

    // 检查全局定位服务是否被允许(“设置”中的“定位服务”)

    if([CLLocationManager locationServicesEnabled]){

        if(!self.locationManager){

            self.locationManager = [[CLLocationManager alloc]init];

            // 设置 CLLocationManagerDelegate 委托对象

            self.locationManager.delegate = self;

            self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;

            // 设备位置更新所需位移的最小距离 500 米

            self.locationManager.distanceFilter = 500;

            

            [self.locationManager startUpdatingLocation];

        }

        else{

            NSLog(@"定位服务不可用");

        }

    }

    //[self.myImageView loadRequest:[NSURLRequest requestWithURL:self.url]];

}


-(void)viewDidAppear:(BOOL)animated

{

    if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone && self.location){

        NSString *lat=[self.location valueForKey:@"lat"];

        NSString *lng=[self.location valueForKey:@"lng"];

        

        NSLog(@"lat=%lf,lng=%lf",[lat doubleValue],[lng doubleValue]);

        CLLocation *location = [[CLLocation alloc]initWithLatitude:[lat doubleValue] longitude:[lng doubleValue]];

        [self putAnnotation: location];

    }

}


- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations

{

    CLLocation *newLocation = [locations lastObject];

    

    //self.myLat.text = [NSString stringWithFormat:@"%@",[NSNumber numberWithDouble:newLocation.coordinate.latitude]];

    //self.myLog.text = [NSString stringWithFormat:@"%@",[NSNumber numberWithDouble:newLocation.coordinate.longitude]];

    

    [self putAnnotation: newLocation];

}


-(void)putAnnotation:(CLLocation *)loc

{

    NSString *city =[self.location valueForKey:@"city"];

    

    if(self.myAnn)

        [self.myMapView removeAnnotation:self.myAnn];

    self.myAnn =[[MKPointAnnotation alloc] init];

    

    self.myAnn.coordinate = loc.coordinate;

    self.myAnn.title = [NSString stringWithFormat:@"ta在这里: %@",city];

    self.myAnn.subtitle = [NSString stringWithFormat:@"lat=%f,lng=%f",loc.coordinate.latitude,loc.coordinate.longitude];

    [self.myMapView addAnnotation:self.myAnn];

    

    MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(loc.coordinate, 1000, 1000);

    MKCoordinateRegion adjustedRegion = [self.myMapView regionThatFits:viewRegion];

    [self.myMapView setRegion:adjustedRegion animated:YES];

}


#pragma mark -

#pragma mark Split view support


- (BOOL)splitViewController:(UISplitViewController *)svc shouldHideViewController:(UIViewController *)vc inOrientation:(UIInterfaceOrientation)orientation

{

    return orientation==UIInterfaceOrientationPortrait;

    //return NO;

}


// 左边视图界面即将被隐藏,此时需在导航栏上设置按钮

- (void)splitViewController: (UISplitViewController*)svc willHideViewController:(UIViewController *)aViewController withBarButtonItem:(UIBarButtonItem*)barButtonItem forPopoverController: (UIPopoverController*)pc {

    

    barButtonItem.title = @"新浪微博";

    [self.navigationItem setLeftBarButtonItem:barButtonItem];

    self.masterPopoverController=pc;

    

}



// Called when the view is shown again in the split view, invalidating the button and popover controller.

- (void)splitViewController: (UISplitViewController*)svc willShowViewController:(UIViewController *)aViewController invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem {

    

    [self.navigationItem setLeftBarButtonItem:nil];

    self.masterPopoverController=nil;

}


@end




问题就出在可以用iphone模拟器成功运行,iphone可以传递数据,但是用ipad模拟器就不能传递数据,请问xa要怎么修改才可以在ipad模拟器也能成功运行,就是会成功传输数据,iphone可以传输数据但是ipad不行。新手上路,不知道怎么有什么api可以调用,iphone可以是因为

MapViewController *v = [segue destinationViewController];传递数据到了下一个页面,那么ipad怎么办,怎么把数据传递到detail页面?


加载中
返回顶部
顶部