我正在尝试使用Objective-C编辑Reachability块中的变量,这是代码:
- (void)testInternetConnection
{
internetReachableFoo = [Reachability reachabilityWithHostname:@"www.google.com"];
// Internet is reachable
internetReachableFoo.reachableBlock = ^(Reachability*reach)
{
// Update the UI on the main thread
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"Connessione ad Internet disponibile");
checkConnection = YES;
if(!lastConnectionState)
{
lastConnectionState = YES;
if(doItemsDownload)
[self displayChoice];
}
});
};
// Internet is not reachable
internetReachableFoo.unreachableBlock = ^(Reachability*reach)
{
// Update the UI on the main thread
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"Connessione ad Internet non disponibile");
checkConnection = NO;
lastConnectionState = NO;
});
};
[internetReachableFoo startNotifier];
}
Run Code Online (Sandbox Code Playgroud)
凡 …
我正在使用Storyboard与autolayout. 在我的中,ViewController我有一个基本结构:一个UIScrollView和一个包含不同标签的 contentView ( UIView)。然后我在viewDidAppearmy 类的方法中添加了不同的元素contentView,然后为了重新计算它的帧大小,我这样做:
- (void)fixContentViewHeight
{
_contentView.frame = CGRectMake(_contentView.frame.origin.x, _contentView.frame.origin.y, _contentView.frame.size.width, operators.frame.origin.y + operators.frame.size.height);
//constraint for contentView height
_contentViewHeight.constant = operators.frame.origin.y + operators.frame.size.height;
}
- (void)fixScrollViewHeight
{
_scrollView.frame = _contentView.frame;
_scrollView.contentSize = _contentView.frame.size;
}
Run Code Online (Sandbox Code Playgroud)
其中operators是 最后放置的元素contentView,它始终位于框架的底部。我在 中调用这两个方法,viewDidAppear它们使视图可滚动,问题是 的框架contentView没有更新,因此最后一个元素始终不可单击(因为它们放置在视图框架之外)。
我究竟做错了什么?为什么scrollView变得可滚动但contentView保持旧框架?
我有一个asyncTask,我想显示一个方法alertDialog何时onPostExecute被触发.我声明了一个Context变量,我在OnCreate方法中初始化它,如:
mContext = this;
Run Code Online (Sandbox Code Playgroud)
然后为了显示alertDialogonPostExecute方法,我使用了以下代码:
AlertDialog.Builder goLogin = new AlertDialog.Builder(mContext);
goLogin.setMessage("test");
goLogin.setCancelable(false);
goLogin.setPositiveButton("ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alertLogin = goLogin.create();
alertLogin.show();
Run Code Online (Sandbox Code Playgroud)
但我得到的是以下错误:
07-10 14:42:09.710: E/AndroidRuntime(12963): java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
07-10 14:42:09.710: E/AndroidRuntime(12963): at android.content.ContextWrapper.getApplicationInfo(ContextWrapper.java:148)
07-10 14:42:09.710: E/AndroidRuntime(12963): at android.view.ContextThemeWrapper.getTheme(ContextThemeWrapper.java:99)
07-10 14:42:09.710: E/AndroidRuntime(12963): at android.app.AlertDialog.resolveDialogTheme(AlertDialog.java:154)
07-10 14:42:09.710: E/AndroidRuntime(12963): at android.app.AlertDialog$Builder.<init>(AlertDialog.java:379)
07-10 14:42:09.710: …Run Code Online (Sandbox Code Playgroud) 我需要用%符号替换字符串中的空格,但我遇到了一些问题,我尝试的是:
imageUrl = imageUrl.replace(' ', "%20");
Run Code Online (Sandbox Code Playgroud)
但它在替换功能中给了我一个错误.
然后:
imageUrl = imageUrl.replace(' ', "%%20");
Run Code Online (Sandbox Code Playgroud)
但它仍然在替换功能中给我一个错误.
我尝试使用unicode符号:
imageUrl = imageUrl.replace(' ', (char) U+0025 + "20");
Run Code Online (Sandbox Code Playgroud)
但它仍然给出错误.
有一个简单的方法吗?