ios - 通过多个 View Controller 从 mysql 数据库下载条目

标签 ios objective-c nsurlconnection

我在 iOS 的 xcode 中处理的代码有一个非常烦人的问题,它快把我逼疯了!

这将需要大量代码解释,因为我需要包含大量代码。

我有一个 viewcontroller正在工作,连接到在线 mysql数据库,并填充 tableview .这一切都很好。

然后我不得不复制这段代码,因为我想从另一个数据库下载并使用另一个 mysql 数据库的坐标填充 map View 。

Screen5ViewController.h / Screen5ViewController.mviewcontroller 的文件与 tableview .

ShopTable.h / ShopTable.m是处理从数据库下载数据的代码。

一切正常。

Screen2ViewController.h / Screen2ViewController.m / MapTable.h / MapTable.m是我复制的文件,从其他数据库下载以填充 mapview .

这根本不起作用,甚至不调用方法 ("NSLog(@"map table called...");") - 所以它甚至不会尝试下载数据。

我不明白为什么这个方法甚至没有被调用?

代码编译和运行没有错误也没有警告。

任何人都可以提供任何帮助,我们将不胜感激。我花了很多时间查看这段代码并尝试了我能想到的一切!

Screen5ViewController.h

#import <UIKit/UIKit.h>
#import "ShopTable.h"

@interface Screen5ViewController : UIViewController <UITableViewDataSource, UITableViewDelegate, ShopTableProtocol> {
}

@property (weak, nonatomic) IBOutlet UITableView *shopTableView;

@end

Screen5ViewController.m

#import "Screen5ViewController.h"

@interface Screen5ViewController () {
ShopTable *_shopTable;
NSArray *_feedItems;
}
@end

@implementation Screen5ViewController

@synthesize

- (void)viewDidLoad {
[super viewDidLoad];

self.title = @"Screen 5";

// Set this view controller object as the delegate and data source for the table view
self.shopTableView.delegate = self;
self.shopTableView.dataSource = self;

// Create array object and assign it to _feedItems variable
_feedItems = [[NSArray alloc] init];

// Create new ShopTable object and assign it to _shopTable variable
_shopTable = [[ShopTable alloc] init];

// Set this view controller object as the delegate for the home model object
_shopTable.delegate = self;

// Call the download items method of the home model object
[_shopTable downloadItems2];
}

-(void)itemsDownloaded2:(NSArray *)items {
// This delegate method will get called when the items are finished downloading

// Set the downloaded items to the array
_feedItems = items;

// Reload the table view
[self.shopTableView reloadData];
}

#pragma mark Table View Delegate Methods

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of feed items (initially 0)
return _feedItems.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// Retrieve cell
NSString *cellIdentifier = @"shopTableCell";
UITableViewCell *myCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

// Get the location to be shown
Location2 *item = _feedItems[indexPath.row];

// Get references to labels of cell

myCell.textLabel.text = item.name;
myCell.detailTextLabel.text = item.address;

return myCell;
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}

@end

购物表.h

#import <Foundation/Foundation.h>

@protocol ShopTableProtocol <NSObject>
- (void)itemsDownloaded2:(NSArray *)items;
@end

@interface ShopTable : NSObject <NSURLConnectionDataDelegate>
@property (nonatomic, weak) id<ShopTableProtocol> delegate;
- (void)downloadItems2;
@end

@interface Location2 : NSObject
@property (nonatomic, strong) NSString *latitude;
@property (nonatomic, strong) NSString *longitude;
@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSString *address;
@end

ShopTable.m

#import "ShopTable.h"

@interface ShopTable() {
NSMutableData *_downloadedData;
}
@end

@implementation ShopTable

- (void)downloadItems2 {
NSLog(@"shop table called...");

// Download the json file
NSURL *jsonFileUrl = [NSURL URLWithString:@"http://www.example.com/shop.php"];

// Create the request
NSURLRequest *urlRequest = [[NSURLRequest alloc] initWithURL:jsonFileUrl];

// Create the NSURLConnection
[NSURLConnection connectionWithRequest:urlRequest delegate:self];
}

#pragma mark NSURLConnectionDataProtocol Methods

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
// Initialize the data object
_downloadedData = [[NSMutableData alloc] init];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
// Append the newly downloaded data
[_downloadedData appendData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
// Create an array to store the locations
NSMutableArray *_locations = [[NSMutableArray alloc] init];

// Parse the JSON that came in
NSError *error;
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:_downloadedData options:NSJSONReadingAllowFragments error:&error];

// Loop through Json objects, create question objects and add them to our questions array
for (int i = 0; i < jsonArray.count; i++) {
    NSDictionary *jsonElement = jsonArray[i];

    // Create a new location object and set its props to JsonElement properties
    Location2 *newLocation = [[Location2 alloc] init];
    newLocation.name = jsonElement[@"title"];
    newLocation.address = jsonElement[@"description"];
    newLocation.latitude = jsonElement[@"Latitude"];
    newLocation.longitude = jsonElement[@"Longitude"];

    // Add this question to the locations array
    [_locations addObject:newLocation];
}

// Ready to notify delegate that data is ready and pass back items
if (self.delegate) {
    [self.delegate itemsDownloaded2:_locations];
}
}

@end

@implementation Location2
@end

Screen2ViewController.h

#import <UIKit/UIKit.h>
#import "MapTable.h"

@interface Screen2ViewController : UIViewController <MapTableProtocol> {
}

@end

Screen2ViewController.m

#import "Screen2ViewController.h"

@interface Screen2ViewController () {
MapTable *_mapTable;
NSArray *_feedItems1;
}
@end

@implementation Screen2ViewController

@synthesize

- (void)viewDidLoad {
[super viewDidLoad];

self.title = @"Screen 2";

NSLog(@"1"); // This is called fine!

// Create array object and assign it to _feedItems variable
_feedItems1 = [[NSArray alloc] init];

NSLog(@"2"); // This is called fine!

// Set this view controller object as the delegate for the home model object
_mapTable.delegate = self;

NSLog(@"3"); // This is called fine!

// Call the download items method of the home model object
[_mapTable downloadItems];
}

NSLog(@"4"); // This is called fine!

-(void)itemsDownloaded:(NSArray *)items {
// This delegate method will get called when the items are finished downloading

// Set the downloaded items to the array
_feedItems1 = items;

NSLog(@"%lu locations downloaded ok!", (unsigned long)_feedItems1.count);
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}

@end

map 表.h

#import <Foundation/Foundation.h>

@protocol MapTableProtocol <NSObject>
- (void)itemsDownloaded:(NSArray *)items;
@end

@interface MapTable : NSObject <NSURLConnectionDataDelegate>
@property (nonatomic, weak) id<MapTableProtocol> delegate;
- (void)downloadItems;
@end

@interface Location : NSObject
@property (nonatomic, strong) NSString *latitude;
@property (nonatomic, strong) NSString *longitude;
@property (nonatomic, strong) NSString *name;
@end

map 表.m

#import "MapTable.h"

@interface MapTable() {
NSMutableData *_downloadedData1;
}
@end

@implementation MapTable

- (void)downloadItems {
NSLog(@"map table called..."); // THIS CODE NEVER GETS CALLED??!!

// Download the json file
NSURL *jsonFileUrl = [NSURL URLWithString:@"http://www.example.com/map.php"];

// Create the request
NSURLRequest *urlRequest = [[NSURLRequest alloc] initWithURL:jsonFileUrl];

// Create the NSURLConnection
[NSURLConnection connectionWithRequest:urlRequest delegate:self];
}

#pragma mark NSURLConnectionDataProtocol Methods

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
// Initialize the data object
_downloadedData1 = [[NSMutableData alloc] init];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
// Append the newly downloaded data
[_downloadedData1 appendData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
// Create an array to store the locations
NSMutableArray *_locations = [[NSMutableArray alloc] init];

// Parse the JSON that came in
NSError *error;
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:_downloadedData1 options:NSJSONReadingAllowFragments error:&error];

// Loop through Json objects, create question objects and add them to our questions array
for (int i = 0; i < jsonArray.count; i++) {
    NSDictionary *jsonElement = jsonArray[i];

    // Create a new location object and set its props to JsonElement properties
    Location *newLocation = [[Location alloc] init];
    newLocation.name = jsonElement[@"firstname"];
    newLocation.latitude = jsonElement[@"mylatitude"];
    newLocation.longitude = jsonElement[@"mylongitude"];

    // Add this question to the locations array
    [_locations addObject:newLocation];
}

// Ready to notify delegate that data is ready and pass back items
if (self.delegate) {
    [self.delegate itemsDownloaded:_locations];
}
}

@end

@implementation Location
@end

最佳答案

在您的Screen2ViewController.h中:

@interface Screen2ViewController : UIViewController <MKMapViewDelegate, MapTableProtocol> {
    IBOutlet MKMapView *mapView;
}

@property (nonatomic, retain) IBOutlet MKMapView *mapView;

@end

Interface Builder 中的 MKMapView 可能链接到 @property,但在 Screen2ViewController.m 中的任何地方您都在使用 mapView(从未初始化的实例变量)而不是self.mapView


此外,与 Screen5ViewController.m 相比,您在 Screen2ViewController-viewDidLoad 方法中缺少 MapTable 初始化:

_mapTable = [[MapTable alloc] init];

关于ios - 通过多个 View Controller 从 mysql 数据库下载条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30574782/

相关文章:

objective-c - NSURLConnection 下载多张图片

ios - 在代码中创建 UIScrollView 子类,我应该在哪里添加 subview ?

ios - 无法快速提取模型类型

ios - 如何在不使用 Markdown 的情况下在 Apple News 格式中使用链接添加(超链接)?

ios - 设置了RepeatInterval的UILocalNotification每周不触发NSWeekdayCalendarUnit

ios - 我如何在 Xcode 中使用 XIB 简单地切换 View Controller ?

ios - 当文本字段在 uitableview 的单元格中时,不调用 touchesBegan

ios - NSURLConnection sendAsynchronousRequest 图像在 ios 4 中崩溃

cocoa - 收到http状态代码204后接收缓存数据

ios - 如何在 map View 中仅对注释设置可访问性?