Skip to content

Commit

Permalink
Merge pull request #99 from franklsf95/master
Browse files Browse the repository at this point in the history
Bring back `title` declaration and silence Xcode warning
  • Loading branch information
sberrevoets committed May 1, 2015
2 parents bd13ca5 + f2e3dd4 commit 7acf065
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 27 deletions.
2 changes: 2 additions & 0 deletions SDCAlertView/Source/SDCAlertController.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ typedef NS_ENUM(NSInteger, SDCAlertControllerActionLayout) {
*/
@property (nonatomic, readonly) NSArray *textFields;

@property (nonatomic, copy) NSString *title;

/**
* The attributed title for the alert. Both \c title and \c attributedTitle can be used to set the title of the alert,
* the title will be set to whichever was called last. That means that setting \c title to \c nil after setting the
Expand Down
55 changes: 28 additions & 27 deletions SDCAlertView/Source/SDCAlertController.m
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ @interface SDCAlertController () <SDCAlertControllerViewDelegate>
@end

@implementation SDCAlertController
@dynamic title;

#pragma mark - Initialization

Expand All @@ -49,47 +50,47 @@ + (instancetype)alertControllerWithAttributedTitle:(NSAttributedString *)attribu

- (instancetype)initWithStyle:(SDCAlertControllerStyle)style {
self = [super init];

if (self) {
_mutableActions = [NSMutableArray array];
_mutableTextFields = [NSMutableArray array];

_preferredStyle = style;
_visualStyle = [[SDCAlertControllerDefaultVisualStyle alloc] init];
_actionLayout = SDCAlertControllerActionLayoutAutomatic;

self.modalPresentationStyle = UIModalPresentationCustom;
self.transitioningDelegate = [[SDCAlertControllerTransitioningDelegate alloc] init];
}

return self;
}

- (instancetype)initWithTitle:(NSString *)title message:(NSString *)message style:(SDCAlertControllerStyle)style {
self = [self initWithStyle:style];

if (self) {
self.title = title;
_message = message;

[self createAlert];
}

return self;
}

- (instancetype)initWithAttributedTitle:(NSAttributedString *)attributedTitle
attributedMessage:(NSAttributedString *)attributedMessage
style:(SDCAlertControllerStyle)style {
self = [self initWithStyle:style];

if (self) {
_attributedTitle = attributedTitle;
_attributedMessage = attributedMessage;

[self createAlert];
}

return self;
}

Expand Down Expand Up @@ -134,7 +135,7 @@ - (void)createAlert {
NSAttributedString *title = self.attributedTitle ? : [self attributedStringForString:self.title];
NSAttributedString *message = self.attributedMessage ? : [self attributedStringForString:self.message];
self.alert = [[SDCAlertControllerView alloc] initWithTitle:title message:message];

self.alert.delegate = self;
self.alert.contentView = [[SDCIntrinsicallySizedView alloc] init];
[self.alert.contentView setTranslatesAutoresizingMaskIntoConstraints:NO];
Expand All @@ -158,30 +159,30 @@ - (UIView *)contentView {

- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardUpdate:) name:UIKeyboardWillChangeFrameNotification object:nil];

self.alert.visualStyle = self.visualStyle;
self.alert.actions = self.actions;
self.alert.actionLayout = self.actionLayout;

[self showTextFieldsInAlertView:self.alert];

self.presentingAlert = YES;

[self.view addSubview:self.alert];
[self.alert sdc_pinWidth:self.visualStyle.width];
[self.alert sdc_centerInSuperview];

[self.alert prepareForDisplay];
}

- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];

// Explanation of why the first responder is set here:
// http://stackoverflow.com/questions/1132784/iphone-have-the-keyboard-slide-into-view-from-the-right-like-when-editing-a-no

if (!self.didAssignFirstResponder) {
[self.textFields.firstObject becomeFirstResponder];
self.didAssignFirstResponder = YES;
Expand All @@ -207,7 +208,7 @@ - (void)alertControllerView:(SDCAlertControllerView *)sender didPerformAction:(S
if (!action.isEnabled || (self.shouldDismissBlock && !self.shouldDismissBlock(action))) {
return;
}

[self dismissWithCompletion:^{
if (action.handler) {
action.handler(action);
Expand All @@ -222,7 +223,7 @@ - (void)addTextFieldWithConfigurationHandler:(void (^)(UITextField *))configurat
textField.font = self.visualStyle.textFieldFont;
textField.autocorrectionType = UITextAutocorrectionTypeNo;
[self.mutableTextFields addObject:textField];

if (configurationHandler) {
configurationHandler(textField);
}
Expand All @@ -235,11 +236,11 @@ - (NSArray *)textFields {
- (void)keyboardUpdate:(NSNotification *)notification {
NSValue *frameValue = notification.userInfo[UIKeyboardFrameEndUserInfoKey];
CGRect frame = [frameValue CGRectValue];

// Apparently, we are in the middle of an animation block when this method is called, meaning there is no need to create one with the right duration
// and curve. Probably a bug on Apple's end, but it does work in our favor here.
self.view.frame = CGRectMake(0, 0, CGRectGetWidth(self.view.frame), CGRectGetMinY(frame));

if (!self.isPresentingAlert) {
[self.alert layoutIfNeeded];
}
Expand Down Expand Up @@ -270,7 +271,7 @@ - (void)dismissWithCompletion:(void (^)(void))completion {
completion();
}
};

[self.legacyAlertView dismissWithClickedButtonIndex:NSIntegerMax animated:YES];
} else {
[self.presentingViewController dismissViewControllerAnimated:YES completion:completion];
Expand All @@ -292,11 +293,11 @@ + (instancetype)showAlertControllerWithTitle:(NSString *)title message:(NSString
+ (instancetype)showAlertControllerWithTitle:(NSString *)title message:(NSString *)message actionTitle:(NSString *)actionTitle subview:(UIView *)subview {
SDCAlertController *controller = [SDCAlertController alertControllerWithTitle:title message:message preferredStyle:SDCAlertControllerStyleAlert];
[controller addAction:[SDCAlertAction actionWithTitle:actionTitle style:SDCAlertActionStyleCancel handler:nil]];

if (subview) {
[controller.contentView addSubview:subview];
}

[controller presentWithCompletion:nil];
return controller;
}
Expand All @@ -319,4 +320,4 @@ - (UITextField *)textFieldAtIndex:(NSInteger)textFieldIndex {
}
}

@end
@end

0 comments on commit 7acf065

Please sign in to comment.