,创建并使用一个UINavigationController
UINavigationController *aNav = [[UINavigationController alloc] init];
然后添加一个视图进去,否则导航栏也没有意义的
UIViewController *aView = [[UIView alloc] initWithNibName: (*xib文件名*)];[aNav pushViewController:aView animated:NO];//导航栏的第一个视图不要动画化
,设置导航栏的左右按钮:
我说过,设置导航栏的按钮并不是去设置导航栏本身,而是当时被导航的视图控制器,比如我们对aView作设置。
设置其标题:
aView.title = @"标题"; //配置一个按钮,我这里是《我的佛典》上的代码UIBarButtonItem *callModalViewButton = [[UIBarButtonItem alloc] initWithTitle:@"经文" style:UIBarButtonItemStyleBordered target:self action:@selector(callModalList)];self.navigationItem.leftBarButtonItem = callModalViewButton;[callModalViewButton release]; //由于本地视图会retain它,所以我们可以release了
可以看到,还是很简单的嘛。
,其他常用方法和属性:
本地视图.navigationItem.leftBarButtonItem //左边栏项目本地视图.navigationItem.rightBarButtonItem //右边栏项目本地视图.navigationItem.backBarButtonItem //后退栏项目本地视图.navigationItem.hidesBackButton //隐藏后退按钮(YES or NO)
在视图的viewWillAppear:方法中添加:
[self.tableView reloadData];
不起作用,viewWillAppear:这个方法根本没有调用
后来发现原来用了UINavigationController后,viewWillAppear方法是没有效果的,要用UINavigationControllerDelegate的– navigationController:willShowViewController:animated:方法才可以达到这个目的。
所以要做到这个,你必须做以下几步:
. 设置代理类
nav.delegate = self;
. 代理类实现UINavigationControllerDelegate Protocol
. 在代理类中添加– navigationController:willShowViewController:animated:方法
如:
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
[self.myTableView reloadData];
}
pushViewController:viewController animated:BOOL
(加载视图控制器)
– 添加指定的视图控制器并予以显示,后接:是否动画显示
popViewControllerAnimated:BOOL
(弹出当前视图控制器)
– 弹出并向左显示前一个视图
popToViewController:viewController animated:BOOL
(弹出到指定视图控制器)
– 回到指定视图控制器, 也就是不只弹出一个
popToRootViewControllerAnimated:BOOL
(弹出到根视图控制器)
– 比如说你有一个“Home”键,也许就会实施这个方法了。
setNavigationBarHidden:BOOL animated:BOOL
(设置导航栏是否显示)
– 如果你想隐藏导航栏,这就是地方了。参照Picasa的WebApp样式
现pushViewController:animated:的不同页面转换特效
. 首先要明确的是,不使用pushViewController的默认动画,所以在调用这个函数时,要将animated设置为NO.
. 使用普通的来CATransition实现转换效果,代码如下:
CATransition *animation = [CATransition animation];
[animation setDuration:0.3];
[animation setType: kCATransitionMoveIn];
[animation setSubtype: kCATransitionFromTop];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault]];
[self.navigationController pushViewController:m_poseAddIssueViewController animated:NO];
[self.navigationController.view.layer addAnimation:animation forKey:nil];
前面讲了本地如何搭建XMPP环境 其实我不懂 也没认真的好好研究原理 这肯定是要看的 不过先上手把代码拼上去 再细细debug 一点点的理解 这两篇的博客也是想帮助那些和我过去一样 认为XMPP是一个多高端多遥远的东西 我想告诉那些人的是 其实就在你手边
因为我也才接触 不敢保证代码规范 以及是否有隐藏的bug 各位如果有缘 看过有帮助就好 没有帮助笑笑就算了
这里需要使用到插件 asmack-jse.jar
首先最重要的是配置连接
public static String host = "192.168.1.101";
public static int port = 5222;
static XMPPConnection connection;
//依葫芦画瓢
static{
XMPPConnection.DEBUG_ENABLED = true;
ConnectionConfiguration configuration = new ConnectionConfiguration(host, port);
connection = new XMPPConnection(configuration);
connection.DEBUG_ENABLED = true;
ProviderManager pm = ProviderManager.getInstance();
configure(pm);
}
注意这里的host 貌似加http会出问题 应该根本就不是用的http协议吧 端口默认是5222 你可以去openfire服务器配置中查询
接下来注册和登录就好说了
注册
Registration reg = new Registration();
reg.setType(IQ.Type.SET);
reg.setTo(MainActivity.connection.getServiceName());
reg.setUsername(usernameEdt.getText().toString());
reg.setPassword(passwdEdt.getText().toString());
reg.addAttribute("android", "geolo_createUser_android");
System.out.println("reg:" + reg);
PacketFilter filter = new AndFilter(new PacketIDFilter(reg
.getPacketID()), new PacketTypeFilter(IQ.class));
PacketCollector collector = MainActivity.connection
.createPacketCollector(filter);
MainActivity.connection.sendPacket(reg);
result = (IQ) collector.nextResult(SmackConfiguration
.getPacketReplyTimeout());
// Stop queuing results
collector.cancel();// 停止请求results(是否成功的结果)
if (result == null) {
Toast.makeText(getApplicationContext(), "服务器没有返回结果",
Toast.LENGTH_SHORT).show();
} else if (result.getType() == IQ.Type.ERROR) {
if (result.getError().toString().equalsIgnoreCase(
"conflict(409)")) {
Toast.makeText(getApplicationContext(), "这个账号已经存在",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "注册失败",
Toast.LENGTH_SHORT).show();
}
} else if (result.getType() == IQ.Type.RESULT) {
Toast.makeText(getApplicationContext(), "恭喜你注册成功",
Toast.LENGTH_SHORT).show();
}
登录
MainActivity.connection.login(usernameEdt.getText().toString(), pwdEdt.getText().toString());
Button loginBtn = (Button) findViewById(R.id.btn_login);
loginBtn.setEnabled(false);
loginBtn.setText("登录成功");
代码太凌乱 上来格式都变了 还是整个项目看着方便
成员函数指针实现的回调
1、回调的类型设置成基类的成员函数,每次set时进行转换
2、回调的参数是menuItem自身
3、回调的实现是 m_pListener->*m_pfnSelector)(this); 即初始化的时候,设置的是监听者(负责处理回调的指针),
回调内的参数是被点击的对象指针,一般情况下 这2个是一致的。
例子:cocos2d-x中的实现
1、CCObject.h
typedef void (CCObject::*SEL_MenuHandler)(CCObject*); // 定义回调类型
#define menu_selector(_SELECTOR) (SEL_MenuHandler)(&_SELECTOR) // 定义转换成合适类型的功能函数, 注意为了可移植性 要用取地址的符号 &
2、CCMenuItem.cpp 中
bool CCMenuItem::initWithTarget(CCObject *rec, SEL_MenuHandler selector) // 设置回调,和负责监听的对象
{
setAnchorPoint(ccp(0.5f, 0.5f));
m_pListener = rec;
m_pfnSelector = selector;
m_bIsEnabled = true;
m_bIsSelected = false;
return true;
}
void CCMenuItem::activate()
{
if (m_bIsEnabled)
{
if (m_pListener && m_pfnSelector)
{
(m_pListener->*m_pfnSelector)(this); // 触发回调,从上一层的menu来调用
}
if (kScriptTypeNone != m_eScriptType)
{
CCScriptEngineManager::sharedManager()->getScriptEngine()->executeMenuItemEvent(this);
}
}
}
3、使用的方式
CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
"CloseNormal.png",
"CloseSelected.png",
this, // 设置listener
menu_selector(HelloWorld::menuCloseCallback));// 设置callback
void HelloWorld::menuCloseCallback(CCObject* pSender) // 真正的回调函数
{
CCDirector::sharedDirector()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
}