手机浏览 RSS 2.0 订阅 膘叔的简单人生 , 腾讯云RDS购买 | 超便宜的Vultr , 注册 | 登陆
浏览模式: 标准 | 列表2014年12月的文章

Yii2 Html组件的默认值

 Yii2 的Html组件越来越觉得容易用了。而且也更方便了,不过设置默认值的时候有一点不方便。比如input的时候都是直接设置placeholder就行了

在Yii1的时候,有一个参数 :htmlOptions => array() , 在Yii2中简化到就剩一个options。。。当然 placeholder 还是可以直接用的

只是在dropdownlist的时候,placeholder就不能用了。这时候的参数是:prompt 

例子:

PHP代码
  1. $form->field($model'lists')->dropDownList(Categories::findAll([1=>1]), ['prompt' => '请选择分类')])  

看到那个findAll([1=>1])没。如果只是用默认的findAll,没有条件的话。不能查询,所以只有1=>1这样。

 

 

uipickerview 学习中遇到的问题点滴

 几个小问题,是在看《精通IOS开发》第六版第七章遇到的

7.8章节:使用自定议选取器创建一个简单游戏

1、P 159 书上写着:添加一个选取器视图,在选取器视图下方添加一个分页

     妹啊,这个分页究竟是什么?开始以为是pagecontrol组件,但看完本章后才发现,这就是一个Label,为什么前后翻译 会不一样??

2、P159最后一段:需要取消选择View设置底部的User Interaction Enabled,这样用户就不能够手动更改刻度盘作弊了

    我擦 ,我把View设置的User Interaction Enabled 的勾取消后,整个页面的button,label什么的全部没有事件了。

    明明只是将pickerview的user interaction enabled的勾取消么,害我浪费了2个小时,我一直在编译、改代码就是想处理为什么我的button不能点击了,点击了之后为什么没反应

3、例程中:

XML/HTML代码
  1. //当然 self.images 是 NSArray  
  2. self.images = @[[UIImage imageNamed:@"seven"] , [UIImage imageNamed:@"apple"]];  

 

但是如果纯粹翻译成swift这是错误的

大小: 36.74 K
尺寸: 500 x 89
浏览: 1548 次
点击打开新窗口浏览全图

最后我改成了:

XML/HTML代码
  1. self.images = ["seven","bar","crown","cherry","lemon","apple"]  

 

然后在调用的时候再创建UIImage,比如:

XML/HTML代码
  1. func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView!) -> UIView {  
  2.       
  3.     var img = self.images?[row] as String ;  
  4.     return  UIImageView(image: UIImage(named: img));  
  5. }  

 

这也算是曲线救国吧,等下次学的深的时候再来看一下这个问题

4、声音

原文中,#import <AudioToolbox/AudioToolbox.h>在swift中就方便了,直接 import AutioToolbox

不过读取声音资源的时候也确实不一样

XML/HTML代码
  1. //.h  
  2. @implementation xxxController{  
  3.     SystemSoundID xxxSoundId  
  4. }  
  5. //.m  
  6. if (xxxSoundId == 0) {  
  7.     NSString *path = [[NSBundle mainBundle] pathForResource:@"xxx" ofType:@"wav"];  
  8.     NSURL *soundURL = [NSURL fileURLWithPath:path];  
  9.     AudioServicesCreateSystemSoundID((__bridge CFURLRef)soundURL , &xxxSoundId );  
  10. }  
  11. //然后就可以播放了  
  12. AudioServicePlaySystemSound(xxxSoundId);  

 

在swift中就不能这么写了。首先,没看到有(__bridge CFURLRef),经过google,我找到了这里:http://stackoverflow.com/questions/24043904/creating-and-playing-a-sound-in-swift,有很多人回答了,有人说flappyswift中有人这样用:

XML/HTML代码
  1. import SpriteKit  
  2. import AVFoundation  
  3.   
  4. class GameScene: SKScene {  
  5.   
  6.     // Grab the path, make sure to add it to your project!  
  7.     var coinSound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("coin", ofType: "wav"))  
  8.     var audioPlayer = AVAudioPlayer()  
  9.   
  10.     // Initial setup  
  11.     override func didMoveToView(view: SKView) {  
  12.         audioPlayer = AVAudioPlayer(contentsOfURL: coinSound, error: nil)  
  13.         audioPlayer.prepareToPlay()  
  14.     }  
  15.   
  16.     // Trigger the sound effect when the player grabs the coin  
  17.     func didBeginContact(contact: SKPhysicsContact!) {  
  18.         audioPlayer.play()  
  19.     }  
  20.   
  21. }  

 

也有人提了个主意,但是说新版的xcode已经不能用了。最后有人贴了段代码:

XML/HTML代码
  1. import AudioToolbox  
  2.   
  3. let chaChingSound: SystemSoundID = createChaChingSound()  
  4.   
  5. class CashRegisterViewController: UIViewController {  
  6.     override func viewWillAppear(animated: Bool) {  
  7.         super.viewWillAppear(animated)  
  8.         AudioServicesPlaySystemSound(chaChingSound)  
  9.     }  
  10. }  
  11.   
  12. func createChaChingSound() -> SystemSoundID {  
  13.     var soundID: SystemSoundID = 0  
  14.     let soundURL = CFBundleCopyResourceURL(CFBundleGetMainBundle(), "Cha-Ching", "aiff", nil)  
  15.     AudioServicesCreateSystemSoundID(soundURL, &soundID)  
  16.     CFRelease(soundURL)  //新版xocde已经不要这一句了,提示自动管理内存,不要你主动释放了
  17.     return soundID  
  18. }  

 

嗯,我就是用的这个。

5、[sef performSelector]

标题的这个方法,在swift中已经没有了,那怎么延迟0.5秒呢?stackoverflow上也有答案:http://stackoverflow.com/questions/24170282/swift-performselector-withobject-afterdelay

最简单的就是:

XML/HTML代码
  1. var timer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: Selector("someSelector"), userInfo: nil, repeats: false)  
  2.   
  3. func someSelector() {  
  4.     // Something after a delay  
  5. }  

 

或者 :

XML/HTML代码
  1. dispatch_after(0.1, dispatch_get_main_queue(), {  
  2.     // your function here  
  3. })  

 

---至此,第7章全部看完。

整个第一章就一个Tabbar和pickerview加datepickerview,却有这么多的问题,而且还有一个就是pickerview中的数据怎么循环?暂时还没有仔细的了解 ,先把整本书读完试试

 

 

swift srand(time(NULL))

随机数在OC中真心简单啊 srand(time(null)),可是这样的代码到了 Swift中就行不通了

首先:swift没有null,time(nil)出来的又是 int,但 srand 的传入参数 又是Uint32.。。所以好让人纠结

不过网上还是有高手,他们说,你可以这样:srand(UInt32(time(nil))),看上去和原来的OC几乎一样,当然 他还提出了

XML/HTML代码
  1. But consider to use arc4random() or its variants instead. From http://nshipster.com/random/:  
  2.   
  3. arc4random does not require an initial seed (with srand or srandom), making it that much easier to use.  
  4. arc4random has a range up to 0x100000000 (4294967296), whereas rand and random top out at RAND_MAX = 0x7fffffff (2147483647).  
  5. rand has often been implemented in a way that regularly cycles low bits, making it more predictable.  
  6. For example,  
  7.   
  8. let x = arc4random_uniform(10)  
  9. generates a random number in the range 0 ... 9.  

 

可是还有人说:

XML/HTML代码
  1. let time = UInt32(NSDate().timeIntervalSinceReferenceDate)  
  2. srand(time)  
  3. print("Random number: \(rand()%10)")  

 

得,还是能用就成

Tags: swift, srand

swift pragma mark

众所周知,大家在OC中对代码进行逻辑组织 用的是#pragma mark - ,生成分隔线

用#pragma mark 函数说明,来生成一个函数的说明X
但在swift中,这个语法就不支持了,毕竟它是属于C的语法,于是就有了新的一些语法,如:// MARK: // FIXME // TODO: 等
 
// MARK: - 生成分隔线
// MARK: 说明
别忘了那个冒号。。。
 
参考 :http://stackoverflow.com/questions/24017316/pragma-mark-in-swift

Tags: swift, pragma

NSDate stringFromDate

小笔记

var dateFormatter = NSDateFormatter();
     dateFormatter.dateFormat = ""; //时间格式,参考 :"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"
 
于是就可以:
    dateFormatter.stringFromDate( NSDate 的值,比如 datePicker.date )
 
简易方法:
XML/HTML代码
  1. func dateformatterDate(date: NSDate) -> NSString  
  2. {  
  3.         var dateFormatter: NSDateFormatterNSDateFormatter = NSDateFormatter()  
  4.         dateFormatter.dateFormat = "MM-dd-yyyy"  
  5.         dateFormatter.timeZone = NSTimeZone(abbreviation: "UTC")  
  6.           
  7.         return dateFormatter.stringFromDate(date)  
  8.           
  9.   
  10. }  
  11.   
  12. //--- Convert Date String to NSDate ---//  
  13.   
  14. func dateformatterDateString(dateString: NSString) -> NSDate?  
  15. {  
  16.         var dateFormatter: NSDateFormatterNSDateFormatter = NSDateFormatter()  
  17.         dateFormatter.dateFormat = "MM-dd-yyyy"  
  18.         dateFormatter.timeZone = NSTimeZone(abbreviation: "UTC")  
  19.           
  20.           
  21.         return dateFormatter.dateFromString(dateString)  
  22.  }  
 
 
Records:13123