- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我的详细页面有一个uipickerview。当我编辑来 self 的 tableviewcell 的相应记录时,我想获得默认选择的值。简而言之,我有一个包含 10 条记录的 TableView 。当我从 tableview 单元格中选择一条记录时,它会导航到详细页面,其中包含三个选择器 View ,每个 View 都有一个组件。我希望选择器 View 默认加载所选值。下面是我如何加载数据的代码。如何在 -(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row 中显示所选值,还有一件事是我正在为此页面使用 UINavigationcontroller?伙计们,你能帮帮我吗
-(void)loadprojects
{
NSString *post =[[NSString alloc] initWithFormat:@"username=%@",[self.projectpicker dataSource]];
//NSString *pickername = [self.projectpicker dataSource];
//NSString *key = @"Da9s^a2Rp4na6R$ikiAsav3Is@niWsa";
//NSString *encrypteduname = [AESCrypt encrypt:pickername password:key];
// Code for Project loading
NSString *projecturltemp = @"http://xxx.xxx/GetAssignedProjects";
NSString *str = [[NSUserDefaults standardUserDefaults] valueForKey:@"UserLoginIdSession"];
NSString *usrid = str;
NSString * projecturl =[NSString stringWithFormat:@"%@/%@",projecturltemp,usrid];
NSURL *url = [NSURL URLWithString:projecturl];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/projectpicker" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/jsonArray" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSError *error = [[NSError alloc] init];
NSHTTPURLResponse *response = nil;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url
cachePolicy:NSURLRequestReturnCacheDataElseLoad
timeoutInterval:30];
// Make synchronous request
urlData = [NSURLConnection sendSynchronousRequest:urlRequest
returningResponse:&response
error:&error];
if ([response statusCode] >= 200 && [response statusCode] < 300)
{
NSString *responseData = [NSJSONSerialization JSONObjectWithData:urlData
options:NSJSONReadingAllowFragments error:&error];
NSArray *entries = [NSJSONSerialization JSONObjectWithData:[responseData dataUsingEncoding:NSUTF8StringEncoding]
options:0 error:&error];
if(!entries)
{
NSLog(@"Error : %@", error);
}
else{
for (NSDictionary *entry in entries) {
projID = [entries valueForKey:@"ID_PROJECT"];
projectNames = [entries valueForKey:@"NM_PROJECT"];
}
randomSelection=[BenefitNames arrayByAddingObjectsFromArray:projectNames];
randomSelectionID = [benID arrayByAddingObjectsFromArray:projID];
_projectpicker.delegate = self;
_projectpicker.dataSource = self;
}
} else {
}
}
-(void)loaddata
{
NSString *eventDate = self.projidstocancel;
[[NSUserDefaults standardUserDefaults] setObject:eventDate forKey:@"Eventdate"];
[[NSUserDefaults standardUserDefaults] synchronize];
NSString *post =[[NSString alloc] initWithFormat:@"username : %@&Password :%@",[self.projectpicker dataSource],[self.projectpicker delegate]];
//NSString *pickername = [self.projectpicker dataSource];
//NSString *key = @"Da9s^a2Rp4na6R$ikiAsav3Is@niWsa";
//NSString *encrypteduname = [AESCrypt encrypt:pickername password:key];
// Code for Project loading
NSString *projecturltemp = @"http://xxx.xxxx/GetDetailsByID";
NSString *str = [[NSUserDefaults standardUserDefaults] valueForKey:@"UserLoginIdSession"];
NSString *usrid = str;
NSString * projecturl =[NSString stringWithFormat:@"%@/%@",projecturltemp,self.hdnRowcount];
NSURL *url = [NSURL URLWithString:projecturl];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/projectpicker" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/jsonArray" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSError *error = [[NSError alloc] init];
NSHTTPURLResponse *response = nil;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url
cachePolicy:NSURLRequestReturnCacheDataElseLoad
timeoutInterval:30];
// Make synchronous request
urlData = [NSURLConnection sendSynchronousRequest:urlRequest
returningResponse:&response
error:&error];
if ([response statusCode] >= 200 && [response statusCode] < 300)
{
NSString *responseData = [NSJSONSerialization JSONObjectWithData:urlData
options:NSJSONReadingAllowFragments error:&error];
NSArray *entries = [NSJSONSerialization JSONObjectWithData:[responseData dataUsingEncoding:NSUTF8StringEncoding]
options:0 error:&error];
if(!entries)
{
NSLog(@"Error : %@", error);
}
else{
for (NSDictionary *entry in entries) {
projectNames = [entries valueForKey:@"NM_PROJECT"];
taskNames = [entries valueForKey:@"TASk_NAME"];
subtaskNames = [entries valueForKey:@"SUBTASK_NAME"];
hdnlblProjects.text = [[entries valueForKey:@"NM_PROJECT"]componentsJoinedByString:@""];
hdnlblTasks.text = [[entries valueForKey:@"TASk_NAME"]componentsJoinedByString:@""];
hdnlblSubTasks.text = [[entries valueForKey:@"SUBTASK_NAME"]componentsJoinedByString:@""];
txthours.text = [[entries valueForKey:@"No_Hours"]componentsJoinedByString:@""];
txtstatus.text = [[entries valueForKey:@"STATUS"]componentsJoinedByString:@""];
lblBenefitsLeaves.text = [[entries valueForKey:@"NM_LEAVE"]componentsJoinedByString:@""];
BenefitNames = [entries valueForKey:@"NM_LEAVE"];
projID = [entries valueForKey:@"ID_PROJECT"];
taskID = [entries valueForKey:@"ID_TASK"];
subtskID = [entries valueForKey:@"ID_SUB_TASK"];
}
lblProjects.text = [[projID valueForKey:@"description"] componentsJoinedByString:@""];
taskstring = [[taskID valueForKey:@"description"] componentsJoinedByString:@""];
subtaskstring = [[subtskID valueForKey:@"description"] componentsJoinedByString:@""];
bentest = [[BenefitNames valueForKey:@"description"] componentsJoinedByString:@""];
hrsdiff1 = [txthours.text floatValue];
}
} else {
}
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
int numberofRows = 0;
if([pickerView isEqual: _projectpicker]){
numberofRows = [randomSelection count];
return numberofRows;
}
else if([pickerView isEqual: _taskspicker]){
numberofRows = [taskNames count];
return numberofRows;
}
else if([pickerView isEqual: _subtaskspicker]){
numberofRows = [subtaskNames count];
return numberofRows;
}
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
NSString *title = @"Test";
if([pickerView isEqual: _projectpicker]){
title = [randomSelection objectAtIndex:row];
return title;
}
else if ([pickerView isEqual: _taskspicker]){
title = [taskNames objectAtIndex:row];
return title;
}
else if ([pickerView isEqual: _subtaskspicker]){
title = [subtaskNames objectAtIndex:row];
return title;
}
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
//NSLog(@"%@",myArrayString);
//NSLog(@"%@",myTaskArrayString);
if([pickerView isEqual: _projectpicker]){
NSNumber *myProjectArrayString = [randomSelectionID objectAtIndex:row];
lblProjects.text = [NSString stringWithFormat:@"%@",myProjectArrayString];
lblProjects.hidden = YES;
lblBenefitsLeaves.text = [randomSelection objectAtIndex:row];
hdnlblProjects.text = [randomSelection objectAtIndex:row];
rownoedit = row;
lblTasks.text = @"0";
hdnlblTasks.text = @"--Select--";
lblSubTasks.text = @"0";
hdnlblSubTasks.text = @"--Select--";
[self loadtasks];
}
//lblProjects.hidden = YES;
else if([pickerView isEqual: _taskspicker])
{
NSNumber *myTaskArrayString = [taskID objectAtIndex:row];
lblTasks.text = [NSString stringWithFormat:@"%@",myTaskArrayString];
lblTasks.hidden = YES;
hdnlblTasks.text = [taskNames objectAtIndex:row];
lblSubTasks.text = @"0";
hdnlblSubTasks.text = @"--Select--";
[self loadsubtasks];
}
else if([pickerView isEqual: _subtaskspicker])
{
NSNumber *mysubtaskArrayString = [subtskID objectAtIndex:row];
lblSubTasks.text = [NSString stringWithFormat:@"%@",mysubtaskArrayString];
lblSubTasks.hidden = YES;
hdnlblSubTasks.text = [subtaskNames objectAtIndex:row];
//lblTasks.text = [taskNames objectAtIndex:[pickerView selectedRowInComponent:1]];
//lblTasks.text = [NSString stringWithFormat:@"%@", myTaskArrayString];
//lblSubTasks.text = [subtaskNames objectAtIndex:[pickerView selectedRowInComponent:2]];
}
}
最佳答案
首先定义一个静态变量并给它你喜欢的初始值加载
例如:static int selectedRow = 3
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:
(NSInteger)row inComponent:(NSInteger)component{
selectedRow = [pickerView selectedRowInComponent:0];
然后在显示 pickerView 的地方放置这一行
[self selectRow:selectedRow inComponent:0 animated:YES];
关于ios - 在uipickerview中显示选中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22832712/
我正在编写一个汽车租赁应用程序,用户必须指定取车和还车位置。 第一步,用户必须选择取货/返回办公室的区域/区域。选择区域后,他必须从该区域可用的办事处列表中进行选择。 为了选择办公室,我使用了 4 个
我正在尝试创建一个文本框,当选择该文本框时,UIPickerView 将打开并提供可供选择的选项。选择后,UIPickerView 将隐藏,所选项目将显示在文本框中。当我运行此程序时,数组中的第一个元
我有多个 UIPickerView 的问题,我在 UICollectionView 上有 8 个,当我滚动第一个 PickerView 和第五个 PickerView 时,它开始自行滚动并采用与第一个
我正在阅读一本 Swift OOP 书籍,我理解实例方法具有将在函数中使用的参数的想法。不清楚的是,在遵循 UIPickerViews 和 UITableViews 的在线教程时,有些方法将 UIPi
我有两个不同的 UIPickerView用户选择第一个 UIPickerView来自 didSelectRow第一个选择器,我得到第二个的输入 UIPickerView .我尝试为单个选择器设置标签,
函数 pickerView() 在 ViewLoad 上调用的代码 private func createPickerView(){ picker = UIPickerView(frame:
我创建了 UIPickerView 如下。现在我想让它 resignFirstResponder 如果用户在其他地方点击然后 UIPickerView 我怎么能实现呢。我已经按照以下方式创建了 UIP
我的 View Controller 中有两个 UIPickerview,pickerView1 和 pickerView2。 我在这里想要实现的是,当我从 pickerView1 中选择一个字符串时
我们正在使用 UIPickerView允许用户从选项列表中进行选择。我们正在添加 UIPickerView作为容器 UIView 的 subview 。然后我们将一个 UITapGestureReco
好吧,我不知道如何处理这个问题。 我有一个 UITextField,我根据 XML 文件的定义将其动态添加到 UITableView,在 XML 文件中,我可以将列表指定为其中一种数据类型,然后我所做
我现在有2个UIPickerView,以后想再添加3个,代码如下: -(void)viewDidLoad{ Pos_x = 5; Pos_y = 70; for (i= 0; i<2; i++) {
UIPickerView 的 selectRow:inComponent:animated:调用pickerView:didSelectRow:inComponent: ? 否则,我可以自己打电话吗?
我有两个按钮,打开一个 UIPickerView @IBOutlet weak var convertFromButton: UIButton! @IBOutlet weak var convertT
祝大家有美好的一天 我尝试在主题行中解释我的查询。据我了解,从代码中看到,所有 pickerview 都在 ViewDidLoad() 中初始化,这意味着它们都有自己的数据集。作为数据验证的一部分(在
我试图在 PickerView 中显示血型,但由于某些原因,选项仅显示为问号! 这是我的代码: class RegisterViewController: UIViewController,
我正在尝试将工具栏添加到 UIPicker . 我已经看到正确的方法是这样: UIToolbar* toolbarWeight= [[UIToolbar alloc] initWithFrame:CG
我将如何向 uipickerview 添加手势事件来更改选项卡?我必须创建一个自定义类,但是,我不知道如何处理 uipickerview。我当前在 uiviews 中存在手势来执行此操作,但我在使用
我想做的是: 显示了一个 UIPickerView。如果用户触摸选定的行,则该行将被锁定(它是多组件选择器),并且其他组件可以自由旋转。如果该行已被锁定并且用户触摸了锁定的行,则该行将被解锁并可以自由
我有两种看法。第一个:第一个 View Controller 第二:SecondViewController FirstView Controller 是我的 UINavigationControll
使用 UISegmentedControl,您可以在不选择任何内容的情况下启动它。如何使用 UIPickerView 做到这一点?看起来显示默认为索引 0。 谢谢 最佳答案 选择器轮必须停留在某些值上
我是一名优秀的程序员,十分优秀!