ios学习笔记(二)xcode 4.3.2下实现基本交互

本文介绍如何使用Xcode创建一个简单的iOS应用,并通过Storyboard编辑界面布局。文中详细讲解了如何设置按钮点击事件以及如何更新界面上的文本标签。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

想必大家都阅读过iphone4与ipad2开发基础教程吧,这本书的xcode与现在的最新版本有些区别,去掉了view base application,只有比较接近的single view application.

首先我们创建一个single view application,注意这里我们不用自动引用计数。

接着我们点击工程列表中的MainStoryboard_iphone.storyboard 来编辑界面:

我们创建两个按钮和一个用来输出的空白文本:

目录结构与视图结构:


2.接着我们写代码来用来与IB界面编辑器来进行连接:在这里我们可以认为IBOutlet是与IB交互的输出,而IBAction则是IB交互的事件。

先编写ViewContoller.h:

#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
{
}
@property (nonatomic,retain) IBOutlet UILabel * statusText;
-(IBAction)buttonPressed:(id)sender;
@end

接着在ViewContoller.m来实现功能:

#import "ViewController.h"
@implementation ViewController
@synthesize statusText;
-(IBAction)buttonPressed:(id)sender

{
    
    NSString *title = [sender titleForState:UIControlStateNormal];//sender用于获得了不同的button的文字 titleForState是根据button状态获取文字的函数
    
    NSString *newText = [[NSString alloc] initWithFormat:@"%@ button pressed.", title];//将title中的文字放入newText中
    
    statusText.text = newText;
    
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    self.statusText = nil;
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
    } else {
        return YES;
    }
}
- (void)dealloc
{
    [statusText release];
    
    [super dealloc];
}
@end

接着我们把这些代码与IB连接起来:

选择MainStoryboard_iphone.storyboard 右击View Controller Scene中的 Buton,选择Touch down与界面的

bottonPressed相连接:


接着将lable和statusText连接在一块:


最后运行程序,点击botton看看效果:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值