【问题标题】:getting two view controllers embedded in the same navigation controller ios将两个视图控制器嵌入到同一个导航控制器 ios
【发布时间】:2014-07-26 21:36:51
【问题描述】:

我是新手,但我有一个关于 Xcode 的问题。我想出了如何让我的视图控制器嵌入到导航控制器中,但是我如何让从第一个视图到第二个视图的 segue 工作?我有两个视图,我需要在它们之间进行切换,但它一直给我一个 thread1 SIGABRT 消息。我尝试制作 segue 模态,但这不起作用。当我尝试将我的第二个视图控制器嵌入导航控制器时,它会创建一个新的导航控制器。有什么建议吗?

#import <UIKit/UIKit.h>

#import <CoreBluetooth/CoreBluetooth.h>







@interface ViewController : UIViewController



@property (strong, nonatomic) CBCentralManager *centralManager;

@property (strong, nonatomic) CBPeripheral *discoveredPerepheral;

@property (strong, nonatomic) NSMutableData *data;

@property (strong, nonatomic) IBOutlet UITextView *textview;

@property (weak, nonatomic) IBOutlet UILabel *isConnected;

@property (weak, nonatomic) IBOutlet UILabel *myPeripherals;

@property (weak, nonatomic) IBOutlet UILabel *aLabel;

@end

#import "ViewController.h"

@implementation ViewController

- (IBAction)connect:(id)sender {

    _centralManager = [[CBCentralManager alloc]initWithDelegate:self queue:nil options:nil];

    _data = [[NSMutableData alloc]init];




}



- (void)viewDidLoad {

}



- (void)viewWillDisappear:(BOOL)animated {

    [super viewWillDisappear:animated];



}



- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}
@end







 #import <UIKit/UIKit.h>
 #import "ViewController.h"
@interface BlueToothViewController : UIViewController

@property (strong, nonatomic) CBCentralManager *centralManager;

@property (strong, nonatomic) CBPeripheral *discoveredPerepheral;

@property (strong, nonatomic) NSMutableData *data;

@property (strong, nonatomic) IBOutlet UITextView *textview;

@property (weak, nonatomic) IBOutlet UILabel *isConnected;

@property (weak, nonatomic) IBOutlet UILabel *myPeripherals;

@property (weak, nonatomic) IBOutlet UILabel *aLabel;



- (void)centralManagerDidUpdateState:(CBCentralManager *)central;

- (void)centralManger:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI;

-(void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error;

-(void)cleanup;

-(void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral;



-(void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error;

-(void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error;

-(void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error;

-(void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error;

-(void)peripheral:(CBPeripheral *)peripheral didUpdateNotificationStateForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error;





@end










#import "BlueToothViewController.h"

@interface BlueToothViewController ()

@end

@implementation BlueToothViewController

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

- (void)viewDidLoad {

}



- (void)viewWillDisappear:(BOOL)animated {

    [super viewWillDisappear:animated];

    [_centralManager stopScan];

}



- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

- (void)centralManagerDidUpdateState:(CBCentralManager *)central {

    //you should test all scenarios

    if (central.state == CBCentralManagerStateUnknown) {

        self.aLabel.text = @"I dont do anything because my state is unknown.";

        return;

    }

    if (central.state == CBCentralManagerStatePoweredOn) {

        //scan for devices

        [_centralManager scanForPeripheralsWithServices:nil options:@{ CBCentralManagerScanOptionAllowDuplicatesKey : @YES }];

        NSLog(@"Scanning Started");

    }

    if (central.state == CBCentralManagerStateResetting) {

        self.aLabel.text = @"I dont do anything because my state is resetting.";

        return;

    }

    if (central.state == CBCentralManagerStateUnsupported) {

        self.aLabel.text = @"I dont do anything because my state is unsupported.";

        return;

    }

    if (central.state == CBCentralManagerStateUnauthorized) {

        self.aLabel.text = @"I dont do anything because my state is unauthorized.";

        return;

    }

    if (central.state == CBCentralManagerStatePoweredOff) {

        self.aLabel.text = @"I dont do anything because my state is powered off.";

        return;

    }   


}

- (void)centralManger:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI {



    NSLog(@"Discovered %@ at %@", peripheral.name, RSSI);

    self.myPeripherals.text = [NSString stringWithFormat:@"%@%@",peripheral.name, RSSI];



    if (_discoveredPerepheral != peripheral) {

        //save a copy of the peripheral

        _discoveredPerepheral = peripheral;

        //and connect

        NSLog(@"Connecting to peripheral %@", peripheral);

        [_centralManager connectPeripheral:peripheral options:nil];



    }

}

-(void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error {



    NSLog(@"Failed to connect");

    [self cleanup];

}

-(void)cleanup {

    //see if we are subscribed to a characteristic on the peripheral

    if (_discoveredPerepheral.services != nil) {

        for (CBService *service in _discoveredPerepheral.services) {

            if (service.characteristics != nil) {

                for (CBCharacteristic *characteristic in service.characteristics) {

                    if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:@"508EFF8E-F541-57EF-BD82-B0B4EC504CA9"]]) {

                        if (characteristic.isNotifying) {

                            [_discoveredPerepheral setNotifyValue:NO forCharacteristic:characteristic];

                            return;

                        }

                    }

                }

            }

        }

    }

    [_centralManager cancelPeripheralConnection:_discoveredPerepheral];

}

-(void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral {

    NSLog(@"Connected");



    [_centralManager stopScan];

    NSLog(@"Scanning stopped");



    self.isConnected.text = [NSString stringWithFormat:@"Connected"];



    [_data setLength:0];



    peripheral.delegate = self;



    [peripheral discoverServices:nil];

}

-(void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error {

    if (error) { [self cleanup];

        return;

    }

    for (CBService *service in peripheral.services) {

        [peripheral discoverCharacteristics:nil forService:service];

    }

    //discover other characteristics

}

-(void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error { if (error) { [self cleanup];

    return;

}

    for (CBCharacteristic *characteristic in service.characteristics) {

        if ([characteristic.UUID isEqual:nil]) {

            [peripheral setNotifyValue:YES forCharacteristic:characteristic];

        }

    }

}

-(void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error {



    if (error) { NSLog(@"Error");

        return;

    }

    NSString *stringFromData = [[NSString alloc]initWithData:characteristic.value encoding:NSUTF8StringEncoding];

    //Have we got everything we need?



    if ([stringFromData isEqualToString:@"EOM"]) {

        [_textview setText:[[NSString alloc]initWithData:self.data encoding:NSUTF8StringEncoding]];



        [peripheral setNotifyValue:NO forCharacteristic:characteristic];



        [_centralManager cancelPeripheralConnection:peripheral];



    }





}

-(void)peripheral:(CBPeripheral *)peripheral didUpdateNotificationStateForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error {



    if ([characteristic.UUID isEqual:nil]) {

        return;

    }

    if (characteristic.isNotifying) {

        NSLog(@"Notification began on %@", characteristic);

    }

    else {

        [_centralManager cancelPeripheralConnection:peripheral];

    }

}

-(void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error {

    _discoveredPerepheral = nil;



    self.isConnected.text = [NSString stringWithFormat:@"Connecting..."];



    [_centralManager scanForPeripheralsWithServices:nil options:@{ CBCentralManagerScanOptionAllowDuplicatesKey : @YES}];



}

 @end

2014-06-05 13:51:28.809 BlindPed[7044:60b] -[ViewController centralManagerDidUpdateState:]:无法识别的选择器发送到实例 0x8cafb50 2014-06-05 13:51:28.812 BlindPed[7044:60b] * 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[ViewController centralManagerDidUpdateState:]:无法识别的选择器发送到实例 0x8cafb50” * 首先抛出调用栈: ( 0 CoreFoundation 0x017f11e4 异常预处理 + 180 1 libobjc.A.dylib 0x015708e5 objc_exception_throw + 44 2核心基础0x0188e243-[NSObject(NSObject)不识别选择器:]+275 3 核心基础 0x017e150b ___forwarding_ + 1019 4 核心基础 0x017e10ee _CF_forwarding_prep_0 + 14 5 核心蓝牙 0x01a46567 -[CBCentralManager xpcConnectionIsInvalid:] + 59 6 CoreBluetooth 0x01a4fd33 62-[CBXpcConnection initWithDelegate:queue:options:sessionType:]_block_invoke20 + 82 7 libdispatch.dylib 0x01c207b8 _dispatch_call_block_and_release + 15 8 libdispatch.dylib 0x01c354d0 _dispatch_client_callout + 14 9 libdispatch.dylib 0x01c23726 _dispatch_main_queue_callback_4CF + 340 10 核心基础 0x0185643e __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE + 14 11 核心基础 0x017975cb __CFRunLoopRun + 1963 12 核心基础 0x017969d3 CFRunLoopRunSpecific + 467 13 核心基础 0x017967eb CFRunLoopRunInMode + 123 14 图形服务 0x038175ee GSEventRunModal + 192 15 图形服务 0x0381742b GSEventRun + 104 16 UIKit 0x00230f9b UIApplicationMain + 1225 17 盲人 0x0000526d 主要 + 141 18 libdyld.dylib 0x01e6a701 开始 + 1 ) libc++abi.dylib:以 NSException 类型的未捕获异常终止 (lldb)

【问题讨论】:

  • 请提供您的代码,以便人们可以适当地帮助您。
  • 抱歉,我正在修改东西,所以有点乱。
  • 您如何尝试从第一个控制器转到第二个控制器?我在您的代码中没有看到任何关于 segue 的内容?你在故事板中做了什么?
  • 是的,我只是在我的第一个视图上有一个按钮,我试图通过 control+click/drag 方法(push)连接到第二个视图。
  • 当该方法仅在 BlueToothViewController 中实现时,您以某种方式在 ViewController 上调用 centralManagerDidUpdateState:。这导致了你的崩溃。

标签: ios xcode


【解决方案1】:

你在使用故事板吗?如果你是,只需拖出一个 UINavigationController(它会附带一个 UITableViewController,但只需单击它并删除它)。然后拖出两个视图控制器,将它们的自定义类更改为您的类。从 UINavigationController 控制并拖动到您的第一个视图控制器(将其设置为根视图控制器)。

您还需要第一个视图控制器上的按钮或其他东西来触发 segue。添加后,从按钮控制拖动到第二个视图控制器并选择“推”segue。它的动作将与触发segue有关。

将没有祖先的箭头(只是一个浮动箭头)拖动到您的 UINavigationController,它应该可以工作。

【讨论】:

  • 是的,我做到了。启动导航视图控制器和两个视图。通过按钮连接的第二个视图。
猜你喜欢
  • 2016-09-06
  • 1970-01-01
  • 2015-03-12
  • 2013-03-12
  • 1970-01-01
  • 1970-01-01
  • 2020-10-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多