Reputation: 27133
I have this code for showing alert view with two needed extra objects:
- (void)leaveCommentButtonPressed
{
UIAlertView *leaveCommentAlert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Leave comment", nil)
message:@""
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"Done", nil];
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 100, 33)];
[textField setBackgroundColor:[UIColor lightGrayColor]];
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 33, 100, 67)];
[textView setBackgroundColor:[UIColor darkGrayColor]];
[view addSubview:textField];
[view addSubview:textView];
CGFloat system_version = [[[UIDevice currentDevice] systemVersion] floatValue];
if (system_version < 7.0) //For Backward compatibility
{
[leaveCommentAlert addSubview:view];
}
else
{
[leaveCommentAlert setValue:view forKey:@"accessoryView"];
}
[leaveCommentAlert show];
}
But my problem is that I can't calculate width of alertView to set width for my text view and text field.
Maybe there are some other answers how to achieve text field and text view. But my idea is to have UIView
with appropriate size.
Upvotes: 1
Views: 1987
Reputation: 1313
If you're just looking to use a default implementation of UIAlertView
, the Apple Docs also state:
Optionally, an alert can contain one or two text fields, one of which can be a secure text-input field. You add text fields to an alert after it is created by setting its alertViewStyle property to one of the styles specified by the UIAlertViewStyle constants. The alert view styles can specify no text field (the default style), one plain text field, one secure text field (which displays a bullet character as each character is typed), or two text fields (one plain and one secure) to accommodate a login identifier and password
See Alert Views
But, as mentioned in a different post, UIAlertView
is deprecated and replaced by UIAlertController
. Fortunately it comes with addTextFieldWithConfigurationHandler:
that allows you to do what you're looking for.
Upvotes: 0
Reputation: 52227
Hic sunt dracones
Subclassing Notes
The UIAlertView class is intended to be used as-is and does not support subclassing. The view hierarchy for this class is private and must not be modified.
You should use a alert view replacement. There are numerous in the web, for example: CXAlertView, DLAlertView or SDAlertView
Upvotes: 1