Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/113.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 从未在UIViewController上调用LoadView_Ios_Objective C_Uiwindow - Fatal编程技术网

Ios 从未在UIViewController上调用LoadView

Ios 从未在UIViewController上调用LoadView,ios,objective-c,uiwindow,Ios,Objective C,Uiwindow,我试图在Objective-C中复制实现 但是从来没有调用过loadView,有人能告诉我为什么吗? 如何修复它 应用程序浮动按钮Windows.m #import "ApplicationFloatingButtonWindow.h" #import "ApplicationFloatingButtonController.h" @interface ApplicationFloatingButtonWindow() #pragma mark - Properties @property

我试图在Objective-C中复制实现

但是从来没有调用过loadView,有人能告诉我为什么吗? 如何修复它

应用程序浮动按钮Windows.m

#import "ApplicationFloatingButtonWindow.h"
#import "ApplicationFloatingButtonController.h"

@interface ApplicationFloatingButtonWindow()

#pragma mark - Properties
@property (strong, nonatomic) UIButton *floatingButton;
@property (strong, nonatomic) ApplicationFloatingButtonController *floatingButtonViewController;

@end

@implementation ApplicationFloatingButtonWindow

#pragma mark - Initialization
- (instancetype)init {
    self = [super init];
    if (self) {
        self.backgroundColor = nil; // make background transparent
    }
    return self;
}

#pragma mark - Gesture Handling
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
    if(self.floatingButton) {
        CGPoint floatingButtonPoint = [self convertPoint:point toView:self.floatingButton];
        return [self.floatingButton pointInside:floatingButtonPoint withEvent:event];
    }
    return [super pointInside:point withEvent:event];
}

@end
#import "ApplicationFloatingButtonController.h"

@interface ApplicationFloatingButtonController ()

@end

@implementation ApplicationFloatingButtonController

#pragma mark - Initialization
- (instancetype)init {
    self = [super initWithNibName:nil bundle:nil];
    if (self) {
        [self setup];
    }
    return self;
}

#pragma mark - Setup
- (void)setup {
    self.window.windowLevel = CGFLOAT_MAX;
    self.window.hidden = NO;
    self.window.rootViewController = self;
    [NSNotificationCenter.defaultCenter addObserver:self selector:@selector(keyboardDidShow) name:UIKeyboardDidShowNotification object:nil];
}

- (void)setupPanGestureRecognizers {
    UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureRecognizerAction:)];
    [self.floatingButton addGestureRecognizer:panGestureRecognizer];
}

#pragma mark - Lifecycle
- (void)loadView {
    UIButton *button = [self createButton];
    UIView *view = [UIView new];
    [view addSubview:button];
    self.view = view;
    self.floatingButton = button;
    [self setupPanGestureRecognizers];
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    [self snapButtonToBestPosition];
}

#pragma mark - Elements Factory
- (UIButton *)createButton {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeInfoDark];
    [button setTitle:@"Floating..." forState:UIControlStateNormal];
    [button setTitleColor:UIColor.blackColor forState:UIControlStateNormal];
    button.backgroundColor = UIColor.whiteColor;
    button.layer.shadowColor = UIColor.blackColor.CGColor;
    button.layer.shadowRadius = 3;
    button.layer.shadowOpacity = 0.8;
    button.layer.shadowOffset = CGSizeZero;
    [button sizeToFit];
    button.frame = CGRectMake(10, 10, 24, 24);
    button.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);;
    return button;
}

#pragma mark - Selectors
- (void)keyboardDidShow {
    // This refreshes the window level and puts it back to the top most view
    self.window.windowLevel = 0;
    self.window.windowLevel = CGFLOAT_MAX;
}

- (void)panGestureRecognizerAction:(UIPanGestureRecognizer *)panGestureRecognizer {
    CGPoint gestureOffset = [panGestureRecognizer translationInView:self.view];
    [panGestureRecognizer setTranslation:CGPointZero inView:self.view];
    CGPoint newButtonCenter = self.floatingButton.center;
    newButtonCenter.x += gestureOffset.x;
    newButtonCenter.y += gestureOffset.y;
    self.floatingButton.center = newButtonCenter;

    if (panGestureRecognizer.state == UIGestureRecognizerStateEnded || panGestureRecognizer.state == UIGestureRecognizerStateCancelled ) {
        [UIView animateWithDuration:0.3 animations:^{
            [self snapButtonToBestPosition];
        }];
    }

}

#pragma mark - Elements Layout and Positioning Methods
- (NSArray *)possibleButtonPositions {

    CGSize buttonSize = self.floatingButton.bounds.size;
    CGRect rect = CGRectInset(self.view.frame, 4 + buttonSize.width / 2, 4 + buttonSize.height / 2);

    NSMutableArray *possiblePositions = [NSMutableArray new];
    [possiblePositions addObject:[NSValue valueWithCGPoint:CGPointMake(CGRectGetMinX(rect), CGRectGetMinY(rect))]];
    [possiblePositions addObject:[NSValue valueWithCGPoint:CGPointMake(CGRectGetMinX(rect), CGRectGetMaxY(rect))]];
    [possiblePositions addObject:[NSValue valueWithCGPoint:CGPointMake(CGRectGetMaxX(rect), CGRectGetMinY(rect))]];
    [possiblePositions addObject:[NSValue valueWithCGPoint:CGPointMake(CGRectGetMaxX(rect), CGRectGetMaxY(rect))]];
    [possiblePositions addObject:[NSValue valueWithCGPoint:CGPointMake(CGRectGetMidX(rect), CGRectGetMidY(rect))]];

    return possiblePositions;
}

- (void)snapButtonToBestPosition {
    CGPoint bestPositionForButton = CGPointZero;
    CGFloat distanceToBestPosition = CGFLOAT_MAX;
    CGPoint buttonCenter = self.floatingButton.center;
    for (NSValue *possibleButtonPositionAsNSValue in [self possibleButtonPositions]) {
        CGPoint possibleButtonPosition = possibleButtonPositionAsNSValue.CGPointValue;
        CGFloat distance = hypot(buttonCenter.x - possibleButtonPosition.x, buttonCenter.y - possibleButtonPosition.y);
        if (distance < distanceToBestPosition) {
            distanceToBestPosition = distance;
            bestPositionForButton = possibleButtonPosition;
        }
    }
    self.floatingButton.center = bestPositionForButton;
}

@end
#import "AppDelegate.h"
#import "ApplicationFloatingButtonController.h"

@interface AppDelegate ()

#pragma mark - Properties
@property (strong, nonatomic) ApplicationFloatingButtonController *floatingButtonController;

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.

    // Configure Floating Button
    self.floatingButtonController = [ApplicationFloatingButtonController new];
    [self.floatingButtonController.floatingButton addTarget:self action:@selector(floatingButtonDidReceiveTouchUpInside) forControlEvents:UIControlEventTouchUpInside];

    return YES;
}

- (void)floatingButtonDidReceiveTouchUpInside {

    UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"My Alert"
                                                                   message:@"This is an alert."
                                                            preferredStyle:UIAlertControllerStyleAlert];

    UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
                                                          handler:^(UIAlertAction * action) {}];

    [alert addAction:defaultAction];
    [self.window.rootViewController presentViewController:alert animated:YES completion:nil];

}

@end
应用程序浮动按钮控制器.h

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

@interface ApplicationFloatingButtonController : UIViewController

#pragma mark - Properties
@property (strong, nonatomic) UIButton *floatingButton;
@property (strong, nonatomic) ApplicationFloatingButtonWindow *window;

@end
解决方案和最终代码如下:

Swift版本

private let window = FloatingButtonWindow() 
FloatingButtonController
类中。您已经在.h文件中创建了属性,但没有在任何地方初始化它。尝试添加

self.window = [ApplicationFloatingButtonWindow new]; 

到您的设置方法。

Swift版本在FloatingButtonController类中有
private let window=FloatingButtonWindow()
:您已经在.h中创建了属性,但没有在任何地方初始化它。尝试添加
self.window=[ApplicationFloatingButtonWindows new]
到您的
设置
方法。谢谢@pbasdf,我没有看到。它正在工作。