簡體   English   中英

從 Swift ViewController 中的 Objective C ViewController 訪問 Objective C 字典

[英]Access an Objective C dictionary from an Objective C ViewController in a Swift ViewController

我在 Swift 中有一個項目,其中我添加了一個用 Objective C 編寫的 ViewController,它涵蓋了它在他的范圍內找到的所有信標(沒有找到 Z818056DBD7E201243206B9C7CD88481C 代碼) 我連接了兩個 ViewController,一切都很好,信標顯示在 Objective C ViewController 的屏幕上。 問題是我需要從 Objective C ViewController 訪問在其中創建的字典(“beaconsDict”),因此我可以在之后將所有信標 RSSI 傳遞給 BLE 網關。 問題是我不知道如何從 Swift ViewController 讀取/訪問 Obj C ViewController 中的字典,因此我可以創建一個 RSSI 列表,之后我將傳遞給 BLE 網關。

(橋接完成,正如我提到的,Obj C ViewController 在我的應用程序中可見,只需按一下按鈕,就可以按預期工作)

Obj C ViewController m 文件:

#import "AIBMainTableViewController.h"
#import "AIBBeaconRegionAny.h"
#import "AIBUtils.h"
#import "AIBDetailViewController.h"

#define  kCellIdentifier @"cellBeaconIdentifier"

@import CoreLocation;

@interface AIBMainTableViewController ()<CLLocationManagerDelegate>

@property(nonatomic, strong) NSDictionary*      beaconsDict;
@property(nonatomic, strong) CLLocationManager* locationManager;
@property(nonatomic, strong) NSArray*           listUUID;
@property(nonatomic)         BOOL               sortByMajorMinor;
@property(nonatomic, retain) CLBeacon*          selectedBeacon;

@end

@implementation AIBMainTableViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.clearsSelectionOnViewWillAppear = NO;
    
    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;
    
    self.listUUID=[[NSArray alloc] init];
    self.beaconsDict=[[NSMutableDictionary alloc] init];
    self.sortByMajorMinor=NO;
    
    AIBBeaconRegionAny *beaconRegionAny = [[AIBBeaconRegionAny alloc] initWithIdentifier:@"Any"];
    [self.locationManager requestWhenInUseAuthorization];
    [self.locationManager startRangingBeaconsInRegion:beaconRegionAny];
        
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc] initWithTitle:@"Order by major/minor" style:UIBarButtonItemStylePlain target:self action:@selector(changeOrdenation)];
}

- (void) changeOrdenation{
    _sortByMajorMinor=!_sortByMajorMinor;
    if (_sortByMajorMinor){
        self.navigationItem.rightBarButtonItem.title=@"Order by distance";
    } else {
        self.navigationItem.rightBarButtonItem.title=@"Order by major/minor";
    }
}

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
    NSLog(@"locationManagerDidChangeAuthorizationStatus: %d", status);
    
    [UIAlertController alertControllerWithTitle:@"Authoritzation Status changed"
                                        message:[[NSString alloc] initWithFormat:@"Location Manager did change authorization status to: %d", status]
                                 preferredStyle:UIAlertControllerStyleAlert];
    
}

- (void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region
{
    NSLog(@"locationManager:%@ didRangeBeacons:%@ inRegion:%@",manager, beacons, region);

    NSMutableArray* listUuid=[[NSMutableArray alloc] init];
    NSMutableDictionary* beaconsDict=[[NSMutableDictionary alloc] init];
    for (CLBeacon* beacon in beacons) {
        NSLog(@"rssi is : %ld", beacon.rssi);
        NSString* uuid=[beacon.proximityUUID UUIDString];
        NSMutableArray* list=[beaconsDict objectForKey:uuid];
        if (list==nil){
            list=[[NSMutableArray alloc] init];
            [listUuid addObject:uuid];
            [beaconsDict setObject:list forKey:uuid];
        }
        [list addObject:beacon];
    }

    [listUuid sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
        NSString* string1=obj1;
        NSString* string2=obj2;
        return [string1 compare:string2];
    }];
    if (_sortByMajorMinor){
        for (NSString* uuid in listUuid){
            NSMutableArray* list=[beaconsDict objectForKey:uuid];
            [list sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
                CLBeacon* b1=obj1;
                CLBeacon* b2=obj2;
                NSComparisonResult r=[b1.major compare:b2.major];
                if (r==NSOrderedSame){
                    r=[b1.minor compare:b2.minor];
                }
                return r;
            }];
        }
    }
    _listUUID=listUuid;
    _beaconsDict=beaconsDict;
    
    [self.tableView reloadData];
}

- (void)locationManager:(CLLocationManager *)manager rangingBeaconsDidFailForRegion:(CLBeaconRegion *)region withError:(NSError *)error
{
     NSLog(@"locationManager:%@ rangingBeaconsDidFailForRegion:%@ withError:%@", manager, region, error);
    
    [UIAlertController alertControllerWithTitle:@"Ranging Beacons fail"
                                        message:[[NSString alloc] initWithFormat:@"Ranging beacons fail with error: %@", error]
                                 preferredStyle:UIAlertControllerStyleAlert];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [_listUUID count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    NSString* key=[_listUUID objectAtIndex:section];
    return [[_beaconsDict objectForKey:key] count];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return [_listUUID objectAtIndex:section];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier forIndexPath:indexPath];
    
    NSString* key=[_listUUID objectAtIndex:[indexPath indexAtPosition:0]];
    CLBeacon* beacon=[[_beaconsDict objectForKey:key] objectAtIndex:[indexPath indexAtPosition:1]];
    cell.textLabel.text=[[NSString alloc] initWithFormat:@"RSSI:%ld", (long)beacon.rssi];
    
    cell.detailTextLabel.text=[[NSString alloc] initWithFormat:@"Distance: %.2fm\tProximity: %@", beacon.accuracy, [AIBUtils stringForProximityValue:beacon.proximity]];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString* key=[_listUUID objectAtIndex:[indexPath indexAtPosition:0]];
    _selectedBeacon=[[_beaconsDict objectForKey:key] objectAtIndex:[indexPath indexAtPosition:1]];

    AIBDetailViewController* detail=[self.storyboard instantiateViewControllerWithIdentifier:@"detail"];
    detail.beacon=_selectedBeacon;
    [self.navigationController pushViewController:detail animated:YES];
}

@end

Obj C ViewController h 文件:

#import <UIKit/UIKit.h>

@interface AIBMainTableViewController : UITableViewController


@end

方法一:

聲明NSDictionary *dictionaryBeacons; 在你的.h 文件中。 初始化和賦值將發生在.m

然后在.swift 文件中,您可以聲明一個屬性,如 var dictBeacons: [String:Any]?

現在在.m 文件中,您可以創建 object 的 swift 視圖 controller 並將 dict 作為參數傳遞

vcObject.dictBeacons = dictionaryBeacons; 然后使用導航 controller 推送它。

方法二:

如果您不想 PUSH controller 並隨機使用它。swift class 然后聲明 @property @property(nonatomic, strong) NSDictionary *dictionaryBeacons; 在 your.h 文件中並使用它 any.swift class 通過創建像 vcObject.dictionaryBeacons 這樣的 VC 的vcObject.dictionaryBeacons

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM