IOS learning: Using UIWindow to customize AlertView (basic code)

Source: Internet
Author: User

[Cpp]
//
// ABCustomAlertView. h
// KnowledgeChoice
//
// Created by on 13-6-19.
// Copyright (c) 2013 DoubleMan. All rights reserved.
// You can use custom controls and proxies ..
 
# Import <UIKit/UIKit. h>
 
@ Interface ABCustomAlertView: UIWindow
 
// Display
-(Void) show;
// Disappear
-(Void) dismiss;
 
@ End

//
// ABCustomAlertView. h
// KnowledgeChoice
//
// Created by on 13-6-19.
// Copyright (c) 2013 DoubleMan. All rights reserved.
// You can use custom controls and proxies ..

# Import <UIKit/UIKit. h>

@ Interface ABCustomAlertView: UIWindow

// Display
-(Void) show;
// Disappear
-(Void) dismiss;

@ End
[Cpp]
//
// ABCustomAlertView. m
// KnowledgeChoice
//
// Created by on 13-6-19.
// Copyright (c) 2013 DoubleMan. All rights reserved.
//
 
# Import "ABCustomAlertView. h"
 
@ Implementation ABCustomAlertView
 
-(Id) initWithFrame :( CGRect) frame
{
Self = [super initWithFrame: frame];
If (self ){
// Initialization code
Self. windowLevel = UIWindowLevelAlert;
// Here, the alpha attribute of UIWindow cannot be set, which will affect the transparency of the sub-view. Here we use a transparent image.
// Sets the shadow to be translucent.
Self. backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed: @ "alert_bg.png"];
 

UIView * view = [[UIView alloc] initWithFrame: CGRectMake (0, 0,200, 80)] autorelease];
View. backgroundColor = [UIColor blackColor];
View. center = CGPointMake (160,240 );
 
[Self addSubview: view];
}

Return self;
}
 
-(Void) show {
[Self makeKeyAndVisible];
}
 
-(Void) dismiss {
[Self resignKeyWindow];
[Self release];
}
 
-(Void) touchesBegan :( NSSet *) touches withEvent :( UIEvent *) event {
// Click to disappear
[Self dismiss];
}
 
-(Void) touchesMoved :( NSSet *) touches withEvent :( UIEvent *) event {

}
 
-(Void) touchesEnded :( NSSet *) touches withEvent :( UIEvent *) event {

}
 
@ End

//
// ABCustomAlertView. m
// KnowledgeChoice
//
// Created by on 13-6-19.
// Copyright (c) 2013 DoubleMan. All rights reserved.
//

# Import "ABCustomAlertView. h"

@ Implementation ABCustomAlertView

-(Id) initWithFrame :( CGRect) frame
{
Self = [super initWithFrame: frame];
If (self ){
// Initialization code
Self. windowLevel = UIWindowLevelAlert;
// Here, the alpha attribute of UIWindow cannot be set, which will affect the transparency of the sub-view. Here we use a transparent image.
// Sets the shadow to be translucent.
Self. backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed: @ "alert_bg.png"];


UIView * view = [[UIView alloc] initWithFrame: CGRectMake (0, 0,200, 80)] autorelease];
View. backgroundColor = [UIColor blackColor];
View. center = CGPointMake (160,240 );

[Self addSubview: view];
}

Return self;
}

-(Void) show {
[Self makeKeyAndVisible];
}

-(Void) dismiss {
[Self resignKeyWindow];
[Self release];
}

-(Void) touchesBegan :( NSSet *) touches withEvent :( UIEvent *) event {
// Click to disappear
[Self dismiss];
}

-(Void) touchesMoved :( NSSet *) touches withEvent :( UIEvent *) event {

}

-(Void) touchesEnded :( NSSet *) touches withEvent :( UIEvent *) event {

}

@ End

This is just the basic idea. You only need to call show and dismiss to display and disappear...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.