ios - 显示日历动态,同时显示月份
<p><p>在这里,我在顶部使用了一个标签来显示今天的月份和年份。而且我还有 6 个标签,它们将显示今天的日期,比如 12 月 31 日到 1 月 5 日。这是代码:</p>
<p><strong>这将显示从下 5 个日期开始的今天日期</strong></p>
<pre><code> NSArray *labelArray = @;
NSDate *today = ;
NSDateFormatter *dateFormatter = [ init];
NSCalendar *calendar = ;
dateFormatter.dateFormat = @"ddMMM";
for (NSInteger i = 0; i < 6; ++i) {
NSDate *nextDate = ;
UILabel *label = (UILabel *)labelArray;
label.text = ;
}
</code></pre>
<p><strong>这将显示当前月份和年份</strong></p>
<pre><code>// show the present month
NSDate *date = ;
// NSDateFormatter *date = [ init];
dateFormatter.dateFormat=@"MMMM";
NSString * monthString = [ capitalizedString];
dateFormatter.dateFormat=@"yyyy";
NSString * yearString = [ capitalizedString];
dateLabel.text = ;
</code></pre>
<p><strong><em>重要提示:</em></strong></p>
<p>我需要的是如下图所示:</p>
<p> <a href="/image/NJZRq.png" rel="noreferrer noopener nofollow"><img src="/image/NJZRq.png" alt="enter image description here"/></a> </p>
<ol>
<li><p>当前日期应该最后显示,并且应该显示之前的 5 个日期。</p></li>
<li><p>我有左右两个按钮。当用户按下右键时,比如现在说它是 12 月 31 日。当用户按右时,它应更改为 1Jan,并应显示从 1 月 1 日起的前 5 个日期。月份 .label 应自动更改为 2016 年 1 月。如下图:</p></li>
</ol>
<p> <a href="/image/ByQhh.png" rel="noreferrer noopener nofollow"><img src="/image/ByQhh.png" alt="enter image description here"/></a> </p>
<p>两个按钮 Action :</p>
<pre><code>- (IBAction)calRight:(id)sender {
NSDateFormatter *dateFormat = [ init];
;
NSDate *tempDate = ;
NSDateComponents *dateComponents = [ init];
;
NSCalendar *calendar = ;
NSDate *newDate = ;
dateLabel.text = ;
}
- (IBAction)calLeft:(id)sender {
NSDateFormatter *dateFormat = [ init];
;
NSDate *tempDate = ;
NSDateComponents *dateComponents = [ init];
;
NSCalendar *calendar = ;
NSDate *newDate = ;
dateLabel.text = ;
}
</code></pre>
<p>我所做的这段代码只是为了改变我尝试过的月份。但不要更改我的日期标签。</p>
<p>请用代码解释一下。</p>
<p><strong>完整编辑:</strong></p>
<pre><code>- (void)viewDidLoad {
;
NSDate *firstLabelDate = ; //make it class variable
NSDate *lastLabelDate = ; //make it class variable
NSArray *labelArray = @; //make it class variable
;
}
- (IBAction)calRight:(id)sender {
NSDateFormatter *dateFormat = [ init];
;
NSDate *tempDate = ;
NSDateComponents *dateComponents = [ init];
;
NSCalendar *calendar = ;
NSDate *newDate = ;
dateLabel.text = ;
;
}
- (IBAction)calLeft:(id)sender {
NSDateFormatter *dateFormat = [ init];
;
NSDate *tempDate = ;
NSDateComponents *dateComponents = [ init];
;
NSCalendar *calendar = ;
NSDate *newDate = ;
dateLabel.text = ;
;
}
- (void)updateDateLabelsGoingForward:(BOOL) goForward andFromDay:(NSDate*) dayFrom {
NSDateFormatter *dateFormatter = [ init];
NSCalendar *calendar = ;
dateFormatter.dateFormat = @"ddMMM";
for (NSInteger i = 0; i < 6; ++i) {
int dateToBeModified = goForward?1:-1;
NSDate *nextDate = ;
UILabel *label = (UILabel *)self.labelArray;
label.text = ;
if(i==5){
self.lastLabelDate = nextDate;
}
if(i==0){
self.firstLabelDate = nextDate;
}
}
}
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>全局定义一个Date对象<code>NSDate *firstdate;</code>并在viewDidLoad firstdate = ;中赋值
<p>现在使用以下方法显示您的日期和月份 从 viewDidLoad <code>;</code></p> 调用此方法
<pre><code>-(void)dateChange
{
NSArray *labelArray = @;
NSDateFormatter *dateFormatter = [ init];
NSCalendar *calendar = ;
dateFormatter.dateFormat = @"ddMMM";
for (NSInteger i = 0; i < 6; ++i) {
NSDate *nextDate = ;
UILabel *label = (UILabel *)labelArray;
label.text = ;
if (i==5) {
dateFormatter.dateFormat=@"MMMM";
NSString * monthString = [ capitalizedString];
dateFormatter.dateFormat=@"yyyy";
NSString * yearString = [ capitalizedString];
dateLabel.text = ;
}
}
}
</code></pre>
<p>现在将您的 calRight 和 calLeft 设置为波纹管</p>
<pre><code>- (IBAction)calRight:(id)sender {
firstdate = ;
;
}
- (IBAction)calLeft:(id)sender {
firstdate = ;
;
}
</code></pre></p>
<p style="font-size: 20px;">关于ios - 显示日历动态,同时显示月份,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/34542105/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/34542105/
</a>
</p>
页:
[1]