IOS之UI  作业

本教程展示了如何在ViewController中使用UIKit创建UIAlertView,并通过UITextField的代理方法将输入的文本传输到UILabel上,同时实现了取消按钮的跳过功能。

题目:点击button,弹出一个UIAlertView,点击确定按钮,把UITextField上输入的文字传输到UILabel上,点击取消按钮,则不传输。


ViewController.h文件

#import


@interface ViewController : UIViewController<</span>UITextFieldDelegate>//声明将要调用的代理协议

//声明aLabelaTextField属性

@property (retain,nonatomic) UILabel *aLabel;

@property (retain,nonatomic) UITextField *aTextField;

@end


ViewController.m文件

 

#import "ViewController.h"


@interface ViewController ()

@end


@implementation ViewController

@synthesize aLabel,aTextField;


- (void)viewDidLoad

{

    [super viewDidLoad];

 

    //此时的self.aLabel相当于.h文件中声明的UILabel *aLabel,两者是一个;如果这边写UILabel *aLabel,那么相当于重新写了一个对象aLabel,此aLabel是一个新的对象,跟之前.h里声明的aLabel无关,所以最终也不会达到效果,若想最终实现把UITextField上输入的文字传输到UILabel上,必须用self.aLabel来调用.h里的内容,从而实现这个功能。

    self.aLabel=[[UILabel alloc]initWithFrame:CGRectMake(100, 20, 80, 40)];//创建标签

    [self.aLabel setText:@"abcdefg"];//设置标签内容

    [self.view addSubview:self.aLabel];//显示标签

    

    self.aTextField=[[UITextField alloc]initWithFrame:CGRectMake(100, 70, 100, 40)];//创建文本框框架

    [self.aTextField setBorderStyle:UITextBorderStyleRoundedRect];

    

    //额外增加的功能

    self.aTextField.delegate=self;

    self.aTextField.placeholder=@"";//文本框内显示

    [self.view addSubview:self.aTextField];//显示文本框


    UIButton *button=[UIButton buttonWithType:UIButtonTypeRoundedRect];

    [button setFrame:CGRectMake(100, 110, 80, 60)];

    [button setTitle:@"go" forState:UIControlStateNormal];//button命名

    [button addTarget:self action:@selector(go) forControlEvents:UIControlEventTouchUpInside];//点击自身

    [self.view addSubview:button];//显示button按钮

    

    //记得释放内存

    [self.aLabel release];

    [self.aTextField release];

}


-(void) go{

    UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:@"消息" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];//对警告视图中的两个按钮进行命名

    [alertView show];//警告视图显示

    

    //释放内存

    [alertView release];

  

}

-(void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

    //实现功能:当用户按了确定键后,此方法被调用。 它做了两件事,一件是把UILabel中的值设置成UITextField中的值,另外一个是关闭虚拟键盘。

    if (buttonIndex==1) {

       //self.aLabel.text=self.aTextField.text;

        self.aLabel.text=self.aTextField.text;

        [self.aTextField resignFirstResponder];

    }

}


- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end







<think>我们要添加一个图片作为图标(通常指favicon),这可以通过在HTML的head部分添加link标签实现。 假设图片文件名为"favicon.ico",并放在与HTML文件相同的目录下。 如果图片是其他格式(如png),则需要指定type属性。 示例代码: ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>页面标题</title> <!-- 添加favicon --> <link rel="icon" href="favicon.ico" type="image/x-icon"> <!-- 对于PNG格式的图标 --> <!-- <link rel="icon" href="favicon.png" type="image/png"> --> <!-- 或者使用通用方式 --> <!-- <link rel="shortcut icon" href="favicon.ico"> --> </head> <body> <h1>欢迎访问我的网站!</h1> <p>这是一个简单的示例页面。</p> </body> </html> ``` 说明: 1. `rel="icon"` 定义了当前文档与链接资源的关系,即图标。 2. `href` 属性指定图标文件的路径。 3. `type` 属性指定图标的MIME类型,对于.ico文件是`image/x-icon`,对于PNG文件是`image/png`。 4. 也可以使用`rel="shortcut icon"`,但根据规范,`rel="icon"`是标准写法,且现代浏览器都支持。 注意:如果网站需要兼容所有设备,可能需要提供多个尺寸的图标,此时可以使用多个link标签,但现代浏览器通常能够自动缩放一个图标适应不同需求。 §§相关问题§§ 1. 如何为不同的设备(如苹果设备)设置特定的图标? 2. 除了.ico格式,还有哪些常用的网站图标格式? 3. 如何创建一个多尺寸的favicon以适应不同的显示需求? 4. 如果网站图标没有显示,可能是什么原因? 5. 在HTML5中,设置网站图标的标准方法是什么?</think># 添加图片作为网站图标的简单实现 在网页中添加图标(通常称为favicon)是提升网站专业度和品牌识别度的重要方式。下面我将展示一个完整的HTML页面,演示如何添加图标并创建一个美观的示例网站。 ```html <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>网站图标示例</title> <!-- 添加网站图标 --> <link rel="icon" href="https://img.icons8.com/ios-filled/50/4CAF50/leaf.png" type="image/png"> <link rel="apple-touch-icon" href="https://img.icons8.com/ios-filled/50/4CAF50/leaf.png"> <style> * { margin: 0; padding: 0; box-sizing: border-box; font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; } body { background: linear-gradient(135deg, #f5f7fa, #e4edf9); color: #333; line-height: 1.6; min-height: 100vh; padding: 20px; } .container { max-width: 1200px; margin: 0 auto; } header { text-align: center; padding: 50px 20px; background: linear-gradient(to right, #4CAF50, #2196F3); color: white; border-radius: 15px; margin-bottom: 40px; box-shadow: 0 10px 30px rgba(0, 0, 0, 0.15); } h1 { font-size: 3.5rem; margin-bottom: 15px; font-weight: 300; } .subtitle { font-size: 1.4rem; max-width: 800px; margin: 0 auto; opacity: 0.9; } .content { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 30px; margin-bottom: 40px; } .card { background: white; border-radius: 15px; padding: 30px; box-shadow: 0 8px 25px rgba(0, 0, 0, 0.08); transition: all 0.3s ease; } .card:hover { transform: translateY(-10px); box-shadow: 0 15px 35px rgba(0, 0, 0, 0.15); } .card h2 { color: #2196F3; margin-bottom: 20px; display: flex; align-items: center; gap: 10px; } .card h2 i { font-size: 1.8rem; } .icon-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(100px, 1fr)); gap: 15px; margin: 20px 0; } .icon-item { text-align: center; padding: 15px; border-radius: 10px; background: #f9f9f9; transition: all 0.3s ease; } .icon-item:hover { background: #e3f2fd; transform: scale(1.05); } .icon-item img { width: 50px; height: 50px; margin-bottom: 10px; } .code-block { background: #2d2d2d; color: #f8f8f2; padding: 25px; border-radius: 10px; margin: 20px 0; overflow-x: auto; font-family: 'Courier New', monospace; } .comment { color: #75715e; } .tag { color: #f92672; } .attribute { color: #a6e22e; } .value { color: #e6db74; } .info-box { background: #e3f2fd; border-left: 4px solid #2196F3; padding: 20px; border-radius: 0 10px 10px 0; margin: 30px 0; } footer { text-align: center; padding: 30px; margin-top: 40px; color: #666; border-top: 1px solid #ddd; } @media (max-width: 768px) { h1 { font-size: 2.5rem; } .subtitle { font-size: 1.1rem; } } </style> </head> <body> <div class="container"> <header> <h1>网站图标实现指南</h1> <p class="subtitle">学习如何在网站中添加和使用图标,提升用户体验和品牌识别度</p> </header> <div class="content"> <div class="card"> <h2><i>📌</i> 添加图标的方法</h2> <p>在HTML中添加网站图标非常简单,只需要在<head>部分添加以下代码:</p> <div class="code-block"> <span class="comment"><!-- 基本favicon --></span><br> <<span class="tag">link</span> <span class="attribute">rel</span>=<span class="value">"icon"</span> <span class="attribute">href</span>=<span class="value">"path/to/favicon.ico"</span> <span class="attribute">type</span>=<span class="value">"image/x-icon"</span>><br><br> <span class="comment"><!-- PNG格式图标 --></span><br> <<span class="tag">link</span> <span class="attribute">rel</span>=<span class="value">"icon"</span> <span class="attribute">href</span>=<span class="value">"path/to/icon.png"</span> <span class="attribute">type</span>=<span class="value">"image/png"</span>><br><br> <span class="comment"><!-- Apple设备专用图标 --></span><br> <<span class="tag">link</span> <span class="attribute">rel</span>=<span class="value">"apple-touch-icon"</span> <span class="attribute">href</span>=<span class="value">"path/to/apple-icon.png"</span>> </div> <div class="info-box"> <strong>最佳实践:</strong> 建议提供多种尺寸的图标(如16x16、32x32、48x48、180x180),以适应不同设备和浏览器的需求。 </div> </div> <div class="card"> <h2><i>💡</i> 图标类型示例</h2> <p>不同格式的图标及其适用场景:</p> <div class="icon-grid"> <div class="icon-item"> <img src="https://img.icons8.com/ios-filled/50/4CAF50/ico.png" alt="ICO格式"> <div>ICO格式</div> <small>传统格式</small> </div> <div class="icon-item"> <img src="https://img.icons8.com/ios-filled/50/2196F3/png.png" alt="PNG格式"> <div>PNG格式</div> <small>透明背景</small> </div> <div class="icon-item"> <img src="https://img.icons8.com/ios-filled/50/FF9800/svg.png" alt="SVG格式"> <div>SVG格式</div> <small>矢量可缩放</small> </div> <div class="icon-item"> <img src="https://img.icons8.com/ios-filled/50/9C27B0/webp.png" alt="WebP格式"> <div>WebP格式</div> <small>高效压缩</small> </div> </div> <div class="info-box"> <strong>现代网站推荐:</strong> 对于现代网站,建议使用SVG格式的图标,它具有体积小、可无限缩放的优势,并且支持多色图标。 </div> </div> </div> <div class="card"> <h2><i>⚙️</i> 高级实现方法</h2> <p>对于需要兼容各种设备的专业网站,可以使用以下代码:</p> <div class="code-block"> <span class="comment"><!-- 多尺寸图标设置 --></span><br> <<span class="tag">link</span> <span class="attribute">rel</span>=<span class="value">"icon"</span> <span class="attribute">href</span>=<span class="value">"/favicon.ico"</span> <span class="attribute">sizes</span>=<span class="value">"any"</span>><br> <<span class="tag">link</span> <span class="attribute">rel</span>=<span class="value">"icon"</span> <span class="attribute">href</span>=<span class="value">"/icon.svg"</span> <span class="attribute">type</span>=<span class="value">"image/svg+xml"</span>><br> <<span class="tag">link</span> <span class="attribute">rel</span>=<span class="value">"apple-touch-icon"</span> <span class="attribute">href</span>=<span class="value">"/apple-touch-icon.png"</span>><br> <<span class="tag">link</span> <span class="attribute">rel</span>=<span class="value">"manifest"</span> <span class="attribute">href</span>=<span class="value">"/site.webmanifest"</span>><br><br> <span class="comment"><!-- 在site.webmanifest文件中 --></span><br> {<br> &nbsp;&nbsp;"icons": [<br> &nbsp;&nbsp;&nbsp;&nbsp;{<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"src": "/icons/icon-192x192.png",<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"type": "image/png",<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"sizes": "192x192"<br> &nbsp;&nbsp;&nbsp;&nbsp;},<br> &nbsp;&nbsp;&nbsp;&nbsp;{<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"src": "/icons/icon-512x512.png",<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"type": "image/png",<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"sizes": "512x512"<br> &nbsp;&nbsp;&nbsp;&nbsp;}<br> &nbsp;&nbsp;]<br> } </div> <div class="info-box"> <strong>PWA应用:</strong> 对于渐进式网页应用(PWA),需要提供多种尺寸的图标,并在manifest.json文件中声明,这样应用在安装到设备主屏幕时可以显示合适的图标。 </div> </div> <footer> <p>© 2023 网站图标实现指南 | 本页面使用纯HTML/CSS实现,图标来自Icons8</p> <p>提示:查看浏览器标签页可以看到添加的绿色叶子图标</p> </footer> </div> </body> </html> ``` ## 添加网站图标的关键点 ### 基本方法 在HTML的`<head>`部分添加以下代码: ```html <!-- 添加基本favicon --> <link rel="icon" href="path/to/favicon.ico" type="image/x-icon"> <!-- 添加PNG格式图标 --> <link rel="icon" href="path/to/icon.png" type="image/png"> <!-- 为Apple设备添加图标 --> <link rel="apple-touch-icon" href="path/to/apple-touch-icon.png"> ``` ### 最佳实践建议 1. **图标格式选择**: - 传统格式:ICO(兼容性好) - 现代格式:PNG(支持透明背景)、SVG(矢量可缩放)、WebP(高效压缩) 2. **多尺寸支持**: - 提供16x16、32x32、48x48等基本尺寸 - 为移动设备提供180x180、192x192、512x512等尺寸 - 为高分辨率屏幕提供2倍尺寸图标 3. **高级实现**: - 使用manifest文件管理PWA应用图标 - 为不同主题提供不同图标(深色/浅色模式) - 使用SVG图标实现响应式设计 4. **工具推荐**: - Favicon生成器:https://realfavicongenerator.net/ - SVG图标库:https://icons8.com/, https://fontawesome.com/ - 图标转换工具:https://cloudconvert.com/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值