小编Byt*_*yte的帖子

错误:只有创建视图层次结构的原始线程才能触及其视图

嗨,谢谢你看我的问题.我是C的中级程序员,但是Android新手.我一直试图让聊天编程工作.假设下面代码中的其他所有内容都能完美运行.我想问的一个问题是,当我尝试从一个运行的线程中调用setText()时,我得到了一个异常.我在这里看了很多网站.发现很多东西,但我真的不明白.请以最简单的方式向我解释,或者如果可能的话,给我一些简单的解决方法.

非常感谢你!!

public class chatter extends Activity {

private String name = "Unknown User";

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);



    final EditText msgToServer = (EditText) findViewById(R.id.msgBox);
    final EditText chatFromServer = (EditText) findViewById(R.id.chatBox); 

    final Button MsgToServer = (Button) findViewById(R.id.sendButton);

    Socket socket = null;
    String ipAddress = "192.168.1.103";
    try {
        InetAddress serverAddr = InetAddress.getByName(ipAddress);
        Socket socketMain = new Socket(serverAddr, 4444);
        socket = socketMain;
    } catch (IOException e) {
        // …
Run Code Online (Sandbox Code Playgroud)

android view hierarchy settext

14
推荐指数
1
解决办法
3万
查看次数

Autolayout约束优先权

我有2 UILabel labelAlabelB宽度为320像素.我创建了约束

H:|-[labelA]-(>=4)-[labelB]-|

不幸的是,labelAlabelB可以是某个大,不适合没有截断.我想确保无论如何labelB都不会被截断.(labelB不会超过160 px).但是,我想尽可能多地使用labelA的可用空间,因此将labelB固定到一半只是浪费空间.

问题:如何确定labelB始终保持其内在规模的优先级?哪里labelA可以被截断.

ios autolayout nslayoutconstraint

11
推荐指数
1
解决办法
6026
查看次数

弹出时,多行UILabel自动布局会折叠为1个线条

以下是演示该问题的示例项目

它是什么?
两个UIViewControllers在堆栈下UINavigationController.除了从一个到另一个呈现之外,每个都与彼此无关.在两个控制器中,都有一个UILabel.每个用途Autolayout.每个标签包含任意数量的行label.numberOfLines = 0.

什么有用?
从viewController A(root)转换到viewController B.B得到分配和发起.B看起来很好.

什么地方出了错?
从过渡BA.在ViewDidDisappear,标签中B决定它将不再显示超过1行,即使它numberOfLines被设置为0.当B被推入堆栈时,其标签仅显示1个衬里而不是多个.

是什么造成的?
不知道.但是在A标签numberOfLines中查找设置为0.如果要删除该行,则标签输入B不会折叠.

问题:但为什么?而且我喜欢A有多行标签,我该怎样克服这个?

代码
A.

@implementation FirstViewController
{
    BugController *_bugController;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Bar Button
    UIBarButtonItem *helpBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"BUG" style:UIBarButtonItemStylePlain target:self action:@selector(bugTapped)];
    [self.navigationItem setLeftBarButtonItem:helpBarButtonItem];

    // A …
Run Code Online (Sandbox Code Playgroud)

uilabel ios autolayout

10
推荐指数
1
解决办法
1612
查看次数

iOS:在rootViewController上设置不正确的方向?

上下文:我正在尝试执行从普通UIViewController.view到自定义的自定义动画UISplitViewController.view.动画应该从左到右显示.

我设置self.window.rootViewController = viewController在哪里viewController是正常的UIViewController.

用户滑动后,会调用以下内容:

UIView *theWindow = [viewController.view superview];

[viewController.view removeFromSuperview];
[theWindow addSubview:self.splitViewController.view];


CATransition *animation = [CATransition animation];
[animation setDuration:0.5];
[animation setType:kCATransitionPush];
[animation setSubtype:kCATransitionFromLeft];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];

[[theWindow layer] addAnimation:animation forKey:@"SwitchToView1"];
Run Code Online (Sandbox Code Playgroud)

当设备处于纵向模式时,一切都很顺利.但是,当设备处于横向模式时,过渡动画的效果就像设备仍处于纵向模式一样.例如:它不是从左边进入,而是从底部进入.两个视图的方向都是完全正确的.只有过渡是奇怪的.

xcode animation orientation ipad ios

7
推荐指数
1
解决办法
2179
查看次数

Android:EditText没有填充ScrollView和RelativeLayout

我一直试图用ScrollView包装我的editText,以便在内容更新时自动滚动它.我发现EditText不会覆盖整个滚动视图的问题,即使宽度和高度都设置为fill_parent.请赐教.谢谢.

这是代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="@+id/sendButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="send"
/>
<EditText
android:id="@+id/msgBox"
android:layout_height="wrap_content" 
android:layout_width="fill_parent" 
android:layout_alignParentBottom="true" 
android:layout_toLeftOf="@+id/sendButton"
android:gravity="left"
android:longClickable="false"
/>
<ScrollView
android:layout_width="fill_parent" 
android:layout_height="fill_parent"
android:layout_above="@+id/msgBox">
<EditText  
android:id="@+id/chatBox"
android:editable="false" 
android:gravity="left|top" 
android:cursorVisible="false" 
android:longClickable="false" 
android:clickable="false"
android:autoLink="all" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"/>
</ScrollView>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

android scrollview relativelayout android-edittext

6
推荐指数
1
解决办法
3826
查看次数

iOS:同一个单词的Bold和Italic

背景:我一直试图用Bold和Italic字体以及普通字体显示一个句子.

问题:如何显示类似"Hello,我的名字是Byte "的内容.请注意,Byte既是粗体又是斜体,而其他单词仍然正常.

我试过了:我认为coreText应该能够做到这一点,我只是无法找到正确的方法来做到这一点.我也使用了TTTAttributeLabel,并且不能使它既粗体又斜体.我有Three20加载,只是不知道使用哪个或什么.Webview不适用于我的背景.


作为对Carles Estevadeordal的回复:

UIWebView *webView = [[UIWebView alloc]initWithFrame:CGRectMake(10, 360, 300, 40)];
[webView setBackgroundColor: [UIColor clearColor]];
[webView loadHTMLString:[NSString stringWithFormat:@"<html><body style=\"background-color: transparent;\">Hello, my name is <b><i>Byte</b></i></body></html>"] baseURL:nil];
[self.view addSubview:webView];
Run Code Online (Sandbox Code Playgroud)

这正是我用过的代码.它显示白色背景.

iphone xcode bold italic ios

6
推荐指数
1
解决办法
9273
查看次数

iOS:解除并呈现ModalViewController而无需访问其Parent ViewController

背景:我想解雇一个我之前提出过的模态视图,然后立即呈现viewController我刚刚用新信息解雇的视频.

问题:如果没有显示指向ViewController第一个ViewController模态的父级的显式指针,我就不会非常成功.我正在尝试编写这个有用viewController的代码,而不会弄乱前面的代码.

可能的领先优势:我一直在尝试一些事情:

1.)试图访问父母ViewController,这时我不知道如何.

2.)一旦获得父母的访问权限,我可以简单地应用以下代码:

UIViewController* toPresentViewController = [[UIViewController alloc] init];
    [self dismissViewControllerAnimated:YES completion:^{
        [parentViewControllerAccessor presentModalViewController:toPresentViewController animated:YES];
}];
Run Code Online (Sandbox Code Playgroud)

理论上,这应该适用于父母的访问权限viewController.我对其他方式持开放态度.

假设:您无权更改父级中的任何代码ViewController.

iphone ipad modalviewcontroller presentmodalviewcontroller ios

6
推荐指数
1
解决办法
2万
查看次数

无法在 iOS 中创建具有文件保护的 SQLite3 数据库

背景:我一直在看 WWDC 2011 - 208 Securing iOS application。他们提到如何使用名为 NSFileProtection 的文件加密来保护我们的数据。虽然我发现提到自动方法 (@38:00) 被窃听,但我希望手动方式 (@37:00) 不是。我尝试对图像使用文件保护,一切都很好。只有我无法创建的 Sqlite。

问题:我尝试创建 SQLite3 数据库(没有核心数据),sqlite3_open_v2并将其SQLITE_OPEN_FILEPROTECTION_COMPLETE作为第三个参数的标志传递。它不会返回SQLITE_OK

代码:

if (sqlite3_open_v2([databasePath UTF8String], &database, SQLITE_OPEN_FILEPROTECTION_COMPLETE, NULL) == SQLITE_OK){
   //everything works
}else{
  //failed
}
Run Code Online (Sandbox Code Playgroud)

更新:
正如 borrrden 所提到的,两者都导致了以下错误代码:( SQLITE_MISUSE库使用不正确)


更新 2: borrrden 的第二条评论是正确的,指出需要这两个标志。它按照以下代码工作:

if (sqlite3_open_v2([databasePath UTF8String], &database, SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE|SQLITE_OPEN_FILEPROTECTION_COMPLETE, NULL) == SQLITE_OK)
Run Code Online (Sandbox Code Playgroud)

database sqlite encryption xcode ios

5
推荐指数
1
解决办法
1209
查看次数

Android:自动向下滚动EditTextView以查找聊天应用

感谢您的关注,我正在创建一个聊天应用程序.它在很大程度上起作用.我遇到的唯一问题是滚动.我使用EditText从服务器发布新消息.

通过方法

msg = msg + "\n" + newMsg
EditText.setText(msg)
Run Code Online (Sandbox Code Playgroud)

我需要尽快将旧文本下的新文本显示出来.

所以我认为最好的方法是在视图更新后自动向下滚动到底部.

有一个简单的方法吗?喜欢布局吗?

再次感谢!

布局代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="@+id/sendButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="send"
/>
    <EditText
android:id="@+id/msgBox"
android:layout_height="wrap_content" 
android:layout_width="fill_parent" 
android:layout_alignParentBottom="true" 
android:layout_toLeftOf="@+id/sendButton"
android:gravity="left"
android:longClickable="false"
/>
<EditText  
android:id="@+id/chatBox"
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:editable="false"
android:layout_above="@+id/msgBox"
android:scrollbars="vertical" 
android:gravity="left|top" 
android:isScrollContainer="true" 
android:cursorVisible="false" 
android:longClickable="false" 
android:clickable="false"
android:autoLink="all"
/>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

android chat autoscroll android-edittext

4
推荐指数
2
解决办法
2万
查看次数

iOS:定位获得未来的self.view.bounds

背景:我想为我的内容的变化和方向设置动画.我的内容是相对于self.view.bounds.

问题:为了使内容与边界一起动画化,我需要知道视图的边界到底是什么.我可以硬编码但我希望找到一种更有活力的方式.

代码:所以所有动画都willRotateToInterfaceOrientation按照以下方式进行:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    //centering the image
    imageView.frame = CGRectMake(self.view.bounds.origin.x + (self.view.bounds.size.width - image.size.width)/2 , self.view.bounds.origin.y + (self.view.bounds.size.height - image.size.height)/2, image.size.width, image.size.height);
    NSLog(@"%d",[[UIDevice currentDevice] orientation]);
    NSLog(@"%f", self.view.bounds.origin.x);
    NSLog(@"%f", self.view.bounds.origin.y);
    NSLog(@"%f", self.view.bounds.size.width);
    NSLog(@"%f", self.view.bounds.size.height);

}
Run Code Online (Sandbox Code Playgroud)

边界方向改变之前.因此,这仅在转换为180度时才有效.如果我要使用didRotateFromInterfaceOrientation,动画将方向改变看起来很糟糕.

objective-c rotation orientation bounds ios

4
推荐指数
1
解决办法
3698
查看次数