ios - 在 uipickerview 中显示选中的值
<p><p>我的详细页面中有一个 uipickerview。当我编辑来 self 的 tableviewcell 的相应记录时,我想获得默认选择的值。简而言之,我有一个包含 10 条记录的表格 View 。当我从 tableview 单元格中选择一条记录时,它会导航到详细页面,该页面包含三个选择器 View ,每个 View 都有一个组件。我希望选择器 View 默认加载选定的值。下面是我如何加载数据的代码。如何在 -(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row 中显示选定的值,还有一件事是我正在为这个页面使用 UINavigationcontroller?小伙伴们可以帮帮我吗</p>
<pre><code>-(void)loadprojects
{
NSString *post =[ initWithFormat:@"username=%@",];
//NSString *pickername = ;
//NSString *key = @"Da9s^a2Rp4na6R$ikiAsav3Is@niWsa";
//NSString *encrypteduname = ;
// Code for Project loading
NSString *projecturltemp = @"http://xxx.xxx/GetAssignedProjects";
NSString *str = [ valueForKey:@"UserLoginIdSession"];
NSString *usrid = str;
NSString * projecturl =;
NSURL *url = ;
NSData *postData = ;
NSString *postLength = ];
NSMutableURLRequest *request = [ init];
;
;
;
;
;
;
NSError *error = [ init];
NSHTTPURLResponse *response = nil;
NSData *urlData=;
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url
cachePolicy:NSURLRequestReturnCacheDataElseLoad
timeoutInterval:30];
// Make synchronous request
urlData = [NSURLConnection sendSynchronousRequest:urlRequest
returningResponse:&response
error:&error];
if ( >= 200 && < 300)
{
NSString *responseData = [NSJSONSerialization JSONObjectWithData:urlData
options:NSJSONReadingAllowFragments error:&error];
NSArray *entries =
options:0 error:&error];
if(!entries)
{
NSLog(@"Error : %@", error);
}
else{
for (NSDictionary *entry in entries) {
projID = ;
projectNames = ;
}
randomSelection=;
randomSelectionID = ;
_projectpicker.delegate = self;
_projectpicker.dataSource = self;
}
} else {
}
}
-(void)loaddata
{
NSString *eventDate = self.projidstocancel;
[ setObject:eventDate forKey:@"Eventdate"];
[ synchronize];
NSString *post =[ initWithFormat:@"username : %@&Password :%@",,];
//NSString *pickername = ;
//NSString *key = @"Da9s^a2Rp4na6R$ikiAsav3Is@niWsa";
//NSString *encrypteduname = ;
// Code for Project loading
NSString *projecturltemp = @"http://xxx.xxxx/GetDetailsByID";
NSString *str = [ valueForKey:@"UserLoginIdSession"];
NSString *usrid = str;
NSString * projecturl =;
NSURL *url = ;
NSData *postData = ;
NSString *postLength = ];
NSMutableURLRequest *request = [ init];
;
;
;
;
;
;
NSError *error = [ init];
NSHTTPURLResponse *response = nil;
NSData *urlData=;
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url
cachePolicy:NSURLRequestReturnCacheDataElseLoad
timeoutInterval:30];
// Make synchronous request
urlData = [NSURLConnection sendSynchronousRequest:urlRequest
returningResponse:&response
error:&error];
if ( >= 200 && < 300)
{
NSString *responseData = [NSJSONSerialization JSONObjectWithData:urlData
options:NSJSONReadingAllowFragments error:&error];
NSArray *entries =
options:0 error:&error];
if(!entries)
{
NSLog(@"Error : %@", error);
}
else{
for (NSDictionary *entry in entries) {
projectNames = ;
taskNames = ;
subtaskNames = ;
hdnlblProjects.text = [componentsJoinedByString:@""];
hdnlblTasks.text = [componentsJoinedByString:@""];
hdnlblSubTasks.text = [componentsJoinedByString:@""];
txthours.text = [componentsJoinedByString:@""];
txtstatus.text = [componentsJoinedByString:@""];
lblBenefitsLeaves.text = [componentsJoinedByString:@""];
BenefitNames = ;
projID = ;
taskID = ;
subtskID = ;
}
lblProjects.text = [ componentsJoinedByString:@""];
taskstring = [ componentsJoinedByString:@""];
subtaskstring = [ componentsJoinedByString:@""];
bentest = [ componentsJoinedByString:@""];
hrsdiff1 = ;
}
} else {
}
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
int numberofRows = 0;
if(){
numberofRows = ;
return numberofRows;
}
else if(){
numberofRows = ;
return numberofRows;
}
else if(){
numberofRows = ;
return numberofRows;
}
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
NSString *title = @"Test";
if(){
title = ;
return title;
}
else if (){
title = ;
return title;
}
else if (){
title = ;
return title;
}
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
//NSLog(@"%@",myArrayString);
//NSLog(@"%@",myTaskArrayString);
if(){
NSNumber *myProjectArrayString = ;
lblProjects.text = ;
lblProjects.hidden = YES;
lblBenefitsLeaves.text = ;
hdnlblProjects.text = ;
rownoedit = row;
lblTasks.text = @"0";
hdnlblTasks.text = @"--Select--";
lblSubTasks.text = @"0";
hdnlblSubTasks.text = @"--Select--";
;
}
//lblProjects.hidden = YES;
else if()
{
NSNumber *myTaskArrayString = ;
lblTasks.text = ;
lblTasks.hidden = YES;
hdnlblTasks.text = ;
lblSubTasks.text = @"0";
hdnlblSubTasks.text = @"--Select--";
;
}
else if()
{
NSNumber *mysubtaskArrayString = ;
lblSubTasks.text = ;
lblSubTasks.hidden = YES;
hdnlblSubTasks.text = ;
//lblTasks.text = ];
//lblTasks.text = ;
//lblSubTasks.text = ];
}
}
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>首先定义一个静态变量,并在加载时为其赋予初始值</p>
<p>ex : <code>static int selectedRow = 3</code></p>
<pre><code>-(void)pickerView:(UIPickerView *)pickerView didSelectRow:
(NSInteger)row inComponent:(NSInteger)component{
selectedRow = ;
</code></pre>
<p>}</p>
<p>然后在你显示pickerView的地方放置这一行</p>
<pre><code>;
</code></pre></p>
<p style="font-size: 20px;">关于ios - 在 uipickerview 中显示选中的值,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/22832712/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/22832712/
</a>
</p>
页:
[1]